如何让NSTimer在满足条件后停止重复

bhz*_*zag 11 nstimer ios swift

我有两个NSTimers,我编程使屏幕上出现一个按钮,然后消失.如何满足条件,如何对其进行编程以停止添加和删除按钮?

这是我的代码:

   var timerRemoveButton = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "removeButton", userInfo: nil, repeats: true)
   var timerAddButton = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "addButton", userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

Ole*_*rov 28

您可以像通常的Objective-C一样使它们无效.所以,当你的条件满足时,只需写:

timerRemoveButton.invalidate()
timerAddButton.invalidate()
Run Code Online (Sandbox Code Playgroud)

这将从NSRunLoop对象中删除您的计时器.