如何让动画重复一定次数?

时间:2017-06-25 02:34:22

标签: ios swift timer uiviewanimation

出于某种原因,当我点击按钮时,它什么也没做,整个应用程序都关闭了。有没有人有什么建议?他们将非常感激。

var start = 1
var timer = Timer()

func test() {
    start += 1
}

@IBAction func start(_ sender: Any) {
    timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(TestViewController.test), userInfo: nil, repeats: true)

    while start <= 10 {
        UIView.animate(withDuration: 0.1, delay: 0, options: [.repeat, .autoreverse], animations: {
            self.buttonLabel.center = CGPoint(x:self.buttonLabel.center.x + 10, y:self.buttonLabel.center.y)
    }, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:3)

有一种更好的方法可以重复动画,请尝试UIView.setAnimationRepeatCount()

@IBAction func start(_ sender: Any) {
    UIView.animate(withDuration: 0.1, delay: 0, options: [.repeat], animations: {
        UIView.setAnimationRepeatCount(10)
        self.buttonLabel.center = CGPoint(x:self.buttonLabel.center.x + 10, y:self.buttonLabel.center.y)
    }, completion: nil)
}