【问题标题】:Swift Version 2 Running Wolf App Issues [duplicate]Swift 版本 2 运行 Wolf 应用程序问题 [重复]
【发布时间】: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。

标签: ios swift swift2


【解决方案1】:

要停止计时器肯定是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")
}

希望对您有所帮助!祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 2015-06-07
    • 1970-01-01
    • 2020-03-23
    • 2021-11-06
    • 1970-01-01
    • 2012-06-22
    • 2017-03-06
    相关资源
    最近更新 更多