斯威夫特:试图让动画逐一发生

时间:2016-05-12 21:55:52

标签: ios swift animation

我有四个带有特定UIImageView动画的函数,我已经在数组中设置了这些函数。我想点击按钮时动画一个接一个地发生,但它们都是同时发生的。

如何制作动画,以便在动画完成后,下一个动画开始,所有4个动画都会动画?

代码:

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

1 个答案:

答案 0 :(得分: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
        })
    }

这可能会破坏其他功能,但它会按顺序执行动画。如果你想能够独立地执行动画,你可以像这样添加一个Bool参数:

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.