无法通过另一个视图控制器 Swift 停止计时器



我为苹果手表开发了一个计时器应用程序。

我现在有两种不同的看法。一个带有实际计时器(计时器控制器(,另一个带有暂停按钮(滑动控制器(。

我正在尝试使用滑动控制器中的按钮的操作来停止/启动计时器控制器中的计时器。

问题是计时器停止了,但第二次按下按钮后计时器不会再次启动。

如果我按下按钮一次,计时器就会停止。如果我再按两次,计时器将再次启动,但在再次按下按钮时不会停止。

对问题可能是什么有什么想法吗?

时间控制器

import WatchKit
import Foundation
import UserNotifications
class TimerController: WKInterfaceController {
@IBOutlet weak var timerOutlet: WKInterfaceTimer! //
@IBOutlet weak var simple_timer_label: WKInterfaceLabel!
var myTimer : Timer?
var duration : TimeInterval = 1 //arbitrary number. 1 seconds
var isPaused = false //flag to determine if it is paused or not
var elapsedTime : TimeInterval = 0.0 //time that has passed between
var number_as_a_timer:Int = 0
var startTime = NSDate()
var dim_date = Date()
var current_minute: Int = 0
var current_hour: Int = 0
var curent_second: Int = 0
var seperate_is_paused_bool: Bool = false
override func awake(withContext context: Any?) {
super.awake(withContext: context)
start_timer()
}
func timeString(time:TimeInterval) -> String {
let hours: Int = Int(time) / 3600
let minutes: Int = Int(time) / 60 % 60
let seconds: Int = Int(time) % 60
let com = NSDateComponents()
com.minute = minutes
com.second = seconds
com.hour = hours
dim_date = NSCalendar.current.date(from: com as 
DateComponents)!
self.timerOutlet.setDate(dim_date)
self.timerOutlet.start()
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
}
func start_timer() {
myTimer = Timer.scheduledTimer(timeInterval: duration, target:
self,selector: #selector(timerDone), userInfo: nil, repeats:
true)
}
@objc private func timerDone(){
//timer done counting down
if !isPaused {
number_as_a_timer += 1
let output:String = self.timeString(time: 
TimeInterval(number_as_a_timer))
self.simple_timer_label.setText(output)
print(output)
}
}
override func willActivate() {
super.willActivate()
NotificationCenter.default.addObserver(self, selector: 
#selector(stop_timer(notification:)), name: .stopTimer, object: 
nil)
}
@objc func stop_timer(notification:NSNotification) {
// Timer is paused. so unpause it and resume countdown
if isPaused {
myTimer = Timer.scheduledTimer(timeInterval: 1, 
target:self, selector: #selector(timerDone), userInfo: nil, 
repeats: true)
self.isPaused = false
print("timer paused: resumming1")
} else {
isPaused = true
print("stoping timer")
//get how much time has passed before they paused it
let paused = NSDate()
elapsedTime += paused.timeIntervalSince(startTime as Date)
//stop watchkit timer on the screen
timerOutlet.stop()
//stop the ticking of the internal timer
myTimer!.invalidate()
}
}
}
extension Notification.Name {
static let stopTimer = Notification.Name("stopTimer")
}

滑动控制器

import WatchKit
import Foundation
import UserNotifications
class SwipeController: WKInterfaceController {
//@IBOutlet weak var myTimer: WKInterfaceTimer!
var timer = TimerController()
var status: Bool = false
override func awake(withContext context: Any?) {
super.awake(withContext: context)
}
@IBAction func PauseButton() {
if timer.myTimer == nil {
print("timer is nil or invalidated")
print("Y: (timer.isPaused)")
let userInfo = ["stop": true] as [String: Bool] // you 
could also transfer data
NotificationCenter.default.post(name: .stopTimer, object: 
nil, userInfo: userInfo)
} else {
print("empty block")
}
}
}

在检查计时器是否暂停时,看起来您从未真正检查过暂停的布尔值在您的 if 语句中是真还是假。

如果已暂停 {<----------->

myTimer = Timer.scheduledTimer(timeInterval: 1, 
target:self, selector: #selector(timerDone), userInfo: nil, 
repeats: true)
self.isPaused = false
print("timer paused: resumming1")

最新更新