在此函数中无效时 NSTimer 不会停止

2023-12-26

我正在尝试使用 NTTimer,但它不起作用。

它是从这里开始的:

timer = NSTimer.scheduledTimerWithTimeInterval(0.003, target: self, selector: "openFrameAnimation", userInfo: nil, repeats: true)

它触发这个函数:

func openFrameAnimation(){
    Swift.print("Animation Goes... ",NSDate().timeIntervalSince1970)
    self.frame = NSRect(x: self.frame.origin.x,
        y: self.frame.origin.y-12,
        width: self.frame.width,
        height: self.frame.height + 12)
    if(self.frame.height >= self.finalFrame.height){
        Swift.print("STOP")
        self.frame = self.finalFrame
        timer.invalidate()
        // timer = nil //ERROR: nil cannot be assigned to type "NSTimer"
    }
    self.needsDisplay = true
}

即使一遍又一遍地打印“STOP”,计时器仍然继续调用openFrameAnimation,我无法停止它。当条件得到验证时,如何停止计时器?

Log:

[...]
*Animation Goes...  1456613095.27813
STOP
[...]
Animation Goes...  1456613095.51233
STOP
Animation Goes...  1456613095.54458
[...]
STOP
Animation Goes...  1456613095.5938
STOP*

Swift 4

  1. add

    var timer = Timer()
    

到班级名列前茅

  1. 设置你的计时器(使用你的原始信息 - 你可能想根据一些建议进行更改,特别是来自@Rob关于帧速率的建议)

    timer = Timer.scheduledTimer(timeInterval: 0.003, target: self, selector: #selector("openFrameAnimation"), userInfo: nil, repeats: true)
    
  2. 停止计时器

        timer.invalidate()
        timer = Timer()
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在此函数中无效时 NSTimer 不会停止 的相关文章

随机推荐