Ios Swift版本2运行Wolf应用程序问题
我在使用Swift v2运行应用程序时遇到一些问题。停止tmrRun是一个问题。我知道如何在目标C中[tmrRun invalidate],但在Swift v2中不知道。任何帮助都将不胜感激Ios Swift版本2运行Wolf应用程序问题,ios,swift,swift2,Ios,Swift,Swift2,我在使用Swift v2运行应用程序时遇到一些问题。停止tmrRun是一个问题。我知道如何在目标C中[tmrRun invalidate],但在Swift v2中不知道。任何帮助都将不胜感激 class ViewController: UIViewController { @IBOutlet var imvWolf: UIImageView! @IBOutlet var btnGo: UIButton! @IBOutlet var btnStop: UIButton!
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.
}
}
停止计时器肯定是
timername.invalidate()
,但请记住还必须停止动画:
timername.invalidate()
isAnimating = false
如果这样做你仍然有问题,我认为问题在于你在哪里输入
请使用以下代码进行尝试:
@IBAction func playButton(sender: AnyObject) {
moveWolf()
}
@IBAction func pauseButton(sender: AnyObject) {
timername.invalidate()
isAnimating = false
}
func moveWolf(){
//Here instead of 0.1 you set the slider value
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("doAnimation"), userInfo: nil, repeats: true)
isAnimating = true
}
func doAnimation(){
if counter == 5 {
counter = 1
}
else{
counter++
}
imatge.image = UIImage(named: "picture\(counter).png")
}
希望对你有帮助!祝你好运 我不知道有多少swift,但是您尝试过tmrRun.invalidate()吗?谢谢,它不起作用,但是调试器建议使用tmrRun!。改为invalidate(),它现在运行,但仍然没有滚动正在运行的wolf的图像。您的计时器被声明为可选计时器,因此
tmrRun!。无效()。另外,计时器将调用更新
,但您尚未将其包括在内。我一直在尝试,但没有成功。我不断收到错误,例如无法将UIImage.type转换为UIImage。