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
})}
}