【发布时间】:2016-02-12 04:34:13
【问题描述】:
我在使用 Swift v2 运行应用程序时遇到了一些问题。停止 tmrRun 是一个问题。我知道如何在 Objective C 中 [tmrRun invalidate] 但在 Swift v2 中不知道。任何帮助,将不胜感激。
class ViewController: UIViewController {
@IBOutlet var imvWolf: UIImageView!
@IBOutlet var btnGo: UIButton!
@IBOutlet var btnStop: UIButton!
@IBOutlet var sliSpeed: UISlider!
var pic = 0
var tmrRun: NSTimer?
@IBAction func startRunnng(sender: UIButton)
{
tmrRun = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)
btnGo.userInteractionEnabled = false
btnStop.userInteractionEnabled = true
sliSpeed.userInteractionEnabled = false
}
@IBAction func stopRunnng(sender: UIButton)
{
[tmrRun invalidate]
btnGo.userInteractionEnabled = true
btnStop.userInteractionEnabled = false
sliSpeed.userInteractionEnabled = true
}
func takeABound() -> ()
{
pic += 1;
if (pic == 8){
pic = 0;
}
}
override func viewDidLoad() {
super.viewDidLoad()
pic = 0;
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
-
我不太懂swift,但是你试过tmrRun.invalidate()吗?
-
谢谢,它没有工作,但调试器随后建议 tmrRun!.invalidate() 代替它现在运行,但仍然没有滚动运行狼的图像。
-
你的计时器被声明为可选的,所以
tmrRun!.invalidate()是正确的。 -
您似乎希望
takeABound更新图片,但没有调用它。另外,计时器会调用update,但你没有包含它。 -
我一直在尝试但没有成功。我不断收到错误,例如无法将 UIImage.type 转换为 UIImage。