相关疑难解决方法(0)

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

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

它从这里开始:

timer = NSTimer.scheduledTimerWithTimeInterval(0.003, target: self, selector: "openFrameAnimation", userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

它激发了这个功能:

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
}
Run Code Online (Sandbox Code Playgroud)

即使反复打印"STOP",计时器仍继续调用openFrameAnimation,我无法阻止它.如果验证条件,我该如何停止计时器?

日志:

[...]
*Animation Goes...  1456613095.27813
STOP
[...]
Animation Goes...  1456613095.51233
STOP
Animation Goes...  1456613095.54458
[...]
STOP
Animation Goes...  1456613095.5938
STOP*
Run Code Online (Sandbox Code Playgroud)

nstimer swift

2
推荐指数
1
解决办法
3011
查看次数

标签 统计

nstimer ×1

swift ×1