2016-05-12 63 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. 
相關問題