简体   繁体   English

停止观看仅在Swift中第一次正常

[英]Stop watch working fine only first time in Swift

I made a stopwatch in swift. 我迅速制作了秒表。 It has three buttons- play, stop, reset. 它具有三个按钮-播放,停止,重置。 it has a display in the middle where I display the seconds passed. 它的中间有个显示屏,显示经过的秒数。

My code runs fine. 我的代码运行良好。 But the watch runs very fast after the stop or reset. 但是手表在停止或重置后运行非常快。

I am not able to figure out what is the fuss. 我无法弄清楚有什么大惊小怪。

please help! 请帮忙!

var time = 0
var timer = NSTimer()
var pause = Bool(false)


@IBOutlet var result: UILabel!

func displayResult() {

    if pause != true
    {
        time++
        result.text = String(time)
    }
}
@IBAction func reset(sender: AnyObject) {

    time = 0

    result.text = String(time)

}


@IBAction func pause(sender: AnyObject) {

    pause = true

}



@IBAction func play(sender: AnyObject) {

    pause = false
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("displayResult") , userInfo: nil, repeats: true)
}

Your pause action should be: 您的pause动作应为:

@IBAction func pause(sender: AnyObject) {

    timer.invalidate()
    pause = true

}

UPD: UPD:

var time = 0
var timer = NSTimer()
var pause = true // 1

func displayResult() {

    if pause != true
    {
        time++
        label.text = String(time)
    }
}
@IBAction func reset(sender: AnyObject) {

    time = 0

    label.text = String(time)
    timer.invalidate() // 2
    pause = true // 3
}


@IBAction func pause(sender: AnyObject) {
    timer.invalidate() // 4
    pause = true

}

@IBAction func play(sender: AnyObject) {

    // 5
    if pause == true {
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("displayResult") , userInfo: nil, repeats: true)
        pause = false
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSTimer仅在第一次工作(快速) - NSTimer work only the first time (Swift) RxSwift:代码第一次工作 - RxSwift: code working only first time TTTAttributedLabel,颜色更改仅在控制器第一次在 Swift 中启动时起作用 - TTTAttributedLabel, change of color works only the first time controller launches in Swift 相机访问请求失败-但仅在第一次访问相机时Swift 3 - Camera Access Request fails - but ONLY the first time the camera is accessed Swift 3 Swift 中是否有仅在第一次打开应用程序时运行的功能? - Is there a function in Swift that only runs during the first time opening the app? #error不支持的体系结构-cdefs.h-Swift BLE项目在Swift 2.3中运行正常,但仅在实际设备上在Swift 3.0中给出错误 - #error Unsupported architecture - cdefs.h - Swift BLE project was working fine in Swift 2.3 but gives error in Swift 3.0 only on real device 苹果手表 - 第一次慢图像动画 - apple watch - slow image animation first time Watch Connectivity首次使用,但此后没有 - Watch Connectivity works the first time but not after that 获取默认日历时出错,仅第一次出现,但此后效果很好 - Error getting default calendar, comes only first time but after that works fine IBAction addTarget:action:forControlEvents:第一次触摸时不起作用,否则很好 - IBAction addTarget:action:forControlEvents: not working when touched for the first time, otherwise fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM