gpt4 book ai didi

swift - 无法通过另一个 View Controller Swift 停止计时器

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:43 25 4
gpt4 key购买 nike

我正在为 Apple Watch 开发计时器应用程序。

我现在有两种不同的看法。一个带有实际计时器 (TimerController),另一个带有暂停按钮 (SwipeController)。

我正在尝试通过 SwipeController 中的按钮停止/启动 TimerController 中的计时器。

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

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

对可能出现的问题有任何想法吗?

时间 Controller

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")
}

滑动 Controller

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")
}
}
}

最佳答案

在检查计时器是否暂停时,您似乎从未真正检查过 isPaused bool 值是真还是假。

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

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

print("timer paused: resumming1")

关于swift - 无法通过另一个 View Controller Swift 停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52784539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com