Swift版本2运行Wolf App问题

时间:2016-02-12 04:34:14

标签: ios swift swift2

Running Wolf App

我在使用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.
    }
}

1 个答案:

答案 0 :(得分:0)

要停止计时器肯定是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")
}
希望对你有所帮助!祝你好运!