2016-05-12 65 views
0

我有一个特定的动画效果的四个funcs UIImageView,我已经在一个数组中设置了这些funcs。当我点击一个按钮时,我想让动画一个接一个地发生,但它们都发生在同一时间。Swift:试图让动画一个接一个地发生

我该怎么做才能让动画完成后动画开始动画?

CODE:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.initAdMobBanner() 

    funcArray = [self.animatePattern1, self.animatePattern2, self.animatePattern3, self.animatePattern4] 

    createArrays() 
    score = 0 
    scoreLabel.text = "\(score)" 
    highScore = NSUserDefaults.standardUserDefaults().integerForKey("HighScore") 

    tapButton.hidden = false 
    goButton.hidden = true 
} 

@IBAction func tapButton(sender: AnyObject) { 
    self.tapButton.hidden = true 
    self.main.shuffleInPlace() 
    print(self.main) 

    UIView.animateWithDuration(1, delay: NSTimeInterval(1.0), options: .Autoreverse, animations: { 
     self.funcArray[self.main[0] - 1]() 
     }) { _ in 

      UIView.animateWithDuration(1, delay: NSTimeInterval(2.0), options: .Autoreverse, animations: { 
       self.funcArray[self.main[1] - 1]() 
       }, completion: { _ in 

        UIView.animateWithDuration(1, delay: NSTimeInterval(3.0), options: .Autoreverse, animations: { 
         self.funcArray[self.main[2] - 1]() 
         }, completion: { _ in 

          UIView.animateWithDuration(1, delay: NSTimeInterval(4.0), options: .Autoreverse, animations: { 
           self.funcArray[self.main[3] - 1]() 
           }, completion: { _ in 
            self.goButton.hidden = false 
          }) 
        }) 
      }) 
    } 

} 


func createArrays() { 
    self.p1.backgroundColor = self.colors[1] 
    self.p2.backgroundColor = self.colors[2] 
    self.p3.backgroundColor = self.colors[3] 
    self.p4.backgroundColor = self.colors[4] 
} 

func animatePattern1() { 
    UIView.animateWithDuration(0.3, animations: {() -> Void in 
     self.p1.transform = CGAffineTransformMakeScale(0.9, 0.9) 
    }) { (finished: Bool) -> Void in 
     UIView.animateWithDuration(0.3, animations: {() -> Void in 
      self.p1.transform = CGAffineTransformIdentity 
     })} 
} 

func animatePattern2() { 
    UIView.animateWithDuration(0.3, animations: {() -> Void in 
     self.p2.transform = CGAffineTransformMakeScale(0.9, 0.9) 
    }) { (finished: Bool) -> Void in 
     UIView.animateWithDuration(0.3, animations: {() -> Void in 
      self.p2.transform = CGAffineTransformIdentity 
     })} 
} 

func animatePattern3() { 
    UIView.animateWithDuration(0.3, animations: {() -> Void in 
     self.p3.transform = CGAffineTransformMakeScale(0.9, 0.9) 
    }) { (finished: Bool) -> Void in 
     UIView.animateWithDuration(0.3, animations: {() -> Void in 
      self.p3.transform = CGAffineTransformIdentity 
     })} 
} 

func animatePattern4() { 
    UIView.animateWithDuration(0.3, animations: {() -> Void in 
     self.p4.transform = CGAffineTransformMakeScale(0.9, 0.9) 
    }) { (finished: Bool) -> Void in 
     UIView.animateWithDuration(0.3, animations: {() -> Void in 
      self.p4.transform = CGAffineTransformIdentity 
     })} 
} 

回答

0

您在使用不同的阵列是难以遵循,和你从一个动画块内部有一个动画块函数调用的是非典型的。你应该尝试简化。你可以这样说:

@IBAction func tapButton(sender: AnyObject) { 
    self.tapButton.hidden = true 
    self.main.shuffleInPlace() 
    print(self.main) 
    self.animatePattern1() 
} 

    func animatePattern1() { 
     UIView.animateWithDuration(0.3, delay: 0.0, options: .Autoreverse, animations: { 
       self.p1.transform = CGAffineTransformMakeScale(0.9, 0.9) 
      }, completion: {_ in 
       self.p1.transform = CGAffineTransformIdentity 
       self.animatePattern2() 
     }) 
    } 

    func animatePattern2() { 
     UIView.animateWithDuration(0.3, delay: 0.0, options: .Autoreverse, animations: { 
      self.p2.transform = CGAffineTransformMakeScale(0.9, 0.9) 
      }, completion: {_ in 
       self.p2.transform = CGAffineTransformIdentity 
       self.animatePattern3() 
     }) 
    } 

    func animatePattern3() { 
     UIView.animateWithDuration(0.3, delay: 0.0, options: .Autoreverse, animations: { 
      self.p3.transform = CGAffineTransformMakeScale(0.9, 0.9) 
      }, completion: {_ in 
       self.p3.transform = CGAffineTransformIdentity 
       self.animatePattern4() 
     }) 
    } 

    func animatePattern4() { 
     UIView.animateWithDuration(0.3, delay: 0.0, options: .Autoreverse, animations: { 
      self.p4.transform = CGAffineTransformMakeScale(0.9, 0.9) 
      }, completion: {_ in 
       self.p4.transform = CGAffineTransformIdentity 
     }) 
    } 

这可能会破坏其他功能,但它会按顺序执行动画。如果您希望能够独立执行动画,则可以为每个像素添加布尔参数:

func animatePattern1(inSequence:Bool) { 
     UIView.animateWithDuration(0.3, delay: 0.0, options: .Autoreverse, animations: { 
       self.p1.transform = CGAffineTransformMakeScale(0.9, 0.9) 
      }, completion: {_ in 
       self.p1.transform = CGAffineTransformIdentity 
       if inSequence { 
        self.animatePattern2(inSequence) 
       } 
     }) 
    } 

    func animatePattern2(inSequence:Bool) { 
     UIView.animateWithDuration(0.3, delay: 0.0, options: .Autoreverse, animations: { 
      self.p2.transform = CGAffineTransformMakeScale(0.9, 0.9) 
      }, completion: {_ in 
       self.p2.transform = CGAffineTransformIdentity 
       if inSequence { 
        self.animatePattern3(inSequence) 
       } 
     }) 
    } 

    //etc. 
相关问题