动画进行得太快了。似乎无法放慢速度

时间:2015-09-26 22:24:45

标签: ios swift

我的动画太快了。我正在做一个模式匹配游戏,我有4个不同颜色的UIViews。我希望一个人眨眼,然后大约一秒钟,让另一个UIView眨眼。我的标签标识符闪烁了视图,我已经为每个视图设置了不同的标签(1,2,3,4)。似乎他们都在同一时间。我已经尝试过调整animateWithDuration函数的值,这似乎没什么帮助。

这是我的打印输出,因此您可以看到它以正确的顺序执行...

gary
3
gary
2
gary
2
gary
3
gary
2



func beginGame() {
    var level = 5

    for _ in 1...level {
        self.randomNumber = Int(arc4random_uniform(4)) + 1
        let originalColor:UIColor = self.view.viewWithTag(randomNumber)!.backgroundColor!
        UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {


            self.view.viewWithTag(self.randomNumber)!.backgroundColor = UIColor.whiteColor()
              self.view.viewWithTag(self.randomNumber)!.backgroundColor = originalColor

            print("gary")


            }, completion: nil)
         print(randomNumber)

        enemyArray.append(randomNumber)

    }
}

//新代码

    func beginGame(){

    var level = 5

    for _ in 1...level {
        self.randomNumber = Int(arc4random_uniform(4)) + 1
        let originalColor:UIColor = self.view.viewWithTag(randomNumber)!.backgroundColor!
        UIView.animateWithDuration(1, delay: 4, options: UIViewAnimationOptions.CurveEaseOut, animations: {


            self.view.viewWithTag(self.randomNumber)!.backgroundColor = UIColor.whiteColor()
              self.view.viewWithTag(self.randomNumber)!.backgroundColor = originalColor

            print("gary")


            }, completion: nil)

         print(randomNumber)

        enemyArray.append(randomNumber)

    }




}

1 个答案:

答案 0 :(得分:0)

倾听别人告诉你的事。如果使用for循环并创建多个动画,其延迟值均为0,则它​​们将同时运行。不要那样做。

相反,请使用更像这样的代码:

let pauseBetweenAnimations = 1.0
for step in 1...level {
  self.randomNumber = Int(arc4random_uniform(4)) + 1
  let originalColor:UIColor = 
    self.view.viewWithTag(randomNumber)!.backgroundColor!
  UIView.animateWithDuration(1, delay: (step - 1) * pauseBetweenAnimations, 
    options: UIViewAnimationOptions.CurveEaseOut, 
    animations:
    //the rest of your code goes here...

这将使每个后续动画在前一个动画之后1秒开始。如果1秒太长,请更改pauseBetweenAnimations的值。