在此函数中无效时 NSTimer 不会停止 [英] NSTimer not stopping when invalidated in this function

查看:55
本文介绍了在此函数中无效时 NSTimer 不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use NTTimer but it doesn't work.

从这里开始:

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,我也无法停止.验证条件后如何停止计时器?

Even if "STOP" is printed over and over again, the timer continues to call openFrameAnimation and I can't stop it. How can I stop timer when condition is verified?

日志:

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

推荐答案

Swift 4

  1. 添加

var timer = Timer()

到班级顶端

  1. 设置您的计时器(使用您的原始信息 - 您可能希望根据一些建议进行更改,尤其是@Rob 关于帧速率的建议)

  1. set your timer (using your original info - you may want to change as per some suggestions, particularly the one from @Rob about the frame rate)

timer = Timer.scheduledTimer(timeInterval: 0.003, target: self, selector: #selector("openFrameAnimation"), userInfo: nil, repeats: true)

  • 停止计时

  • stop the timer

        timer.invalidate()
        timer = Timer()
    

  • 这篇关于在此函数中无效时 NSTimer 不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆