在iOS动画中,可以对不同的控件分别进行设置动画效果,并且设置不同的时间延迟。并且要注意闭包函数的使用。下面我们来实现一下。
(1)在Main.storyboard中拖入三个不同颜色的View控件,放置在不同位置,并且绑定到代码中,如图:
(3)然后在代码中实现如下:
import UIKit class PositionViewController: UIViewController { @IBOutlet weak var greenSquare: UIView! @IBOutlet weak var redSquare: UIView! @IBOutlet weak var blueSquare: UIView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func viewDidAppear(animated: Bool) { //闭包函数的定义; //注意调用动画的方法中的animations,completion使用的都是闭包函数;可以直接在外面定义,里面调用,这样代码更加清晰; func completeGreen(v:Bool){ println("Green Completion") } func completeRed(v:Bool){ println("Red Completion") } func completeBlue(v:Bool){ println("Blue Completion") } func animGreen(){ self.greenSquare.center.x = self.view.bounds.width - self.greenSquare.center.x } func animRed(){ self.redSquare.center.y = self.view.bounds.height - self.redSquare.center.y } func animBlue(){ self.blueSquare.center.y = self.view.bounds.height - self.blueSquare.center.y self.blueSquare.center.x = self.view.bounds.width - self.blueSquare.center.x } //参数delay表示延迟,第一个参数表示动画时间; //注意调用闭包函数; UIView.animateWithDuration(1, delay: 0, options: nil, animations: animGreen, completion: completeGreen) UIView.animateWithDuration(1, delay: 0.5, options: nil, animations: animRed, completion: completeRed) UIView.animateWithDuration(1, delay: 1, options: nil, animations: animBlue, completion: completeBlue) /* 参数提示中: ()->Void:表示参数为空,返回值为Void,必须要实现这个闭包函数; <#((Bool) -> Void)?##(Bool) -> Void#>:表示参数为Bool类型,返回值为Void,后面的?表示这个闭包函数可以为空; */ // UIView.animateWithDuration(<#duration: NSTimeInterval#>, delay: <#NSTimeInterval#>, options: <#UIViewAnimationOptions#>, animations: <#() -> Void##() -> Void#>, completion: <#((Bool) -> Void)?##(Bool) -> Void#>) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!