iOS项目开发实战——多个视图的平移动画与闭包函数的

作者:襄阳西继迅达电梯有限公司  发布时间:2017-09-06 10:09:48
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. } }
(4)对三个不同的色块设置了延迟,注意不要勾选“Use Auto Layout”.实现效果如下:

\

\

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:襄阳SEO http://xiangyang.raoyu.net

  • 上一篇:PHP基础学习之文件操作
  • 下一篇:最后一页
  •