8.24 Animation 简单动画
2016-09-07 本文已影响11人
jayck
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var redView: UIView!
@IBOutlet weak var blueView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func didStart(sender: UIButton) {
// UIView.animateWithDuration(3) {
// self.redView.frame = CGRect(x: 300, y: 300, width: 100, height: 100) 更改红色视图坐标及颜色
// self.redView.backgroundColor = UIColor.blueColor()
//
// }
// 执行时长,执行的内容,结束后做什么
// UIView.animateWithDuration(5, animations: {
// self.blueView.frame = CGRect(x: 50, y: 50, width: 100, height: 100) 更改蓝色视图坐标
// }) { (completion) in
// print("动画执行完了")
// }
时长,延迟多少秒执行,执行的可选方法,执行的内容
// UIView.animateWithDuration(5, delay: 3, options: [.CurveEaseInOut, .Autoreverse], animations: {
// self.blueView.backgroundColor = UIColor.yellowColor()
// self.blueView.frame = CGRect(x: 50, y: 50, width: 100, height: 100)
// }) { (completion) in
// print("The End")
// }
注释了的代码为写法之一,编译运行,看到两个视图颜色位置均有变化
Paste_Image.png Paste_Image.png Paste_Image.png使用UIVIew.setAnimation的方法做动画效果
// 1.开始设置动画
UIView.beginAnimations("first", context: nil)
// 2. 设置动画属性
UIView.setAnimationDuration(5) //时长几秒
UIView.setAnimationRepeatCount(2) //重复几次
UIView.setAnimationDelegate(self) //设置代理方法所在的对象
UIView.setAnimationWillStartSelector(#selector(animateDidStart(_:context:)))
UIView.setAnimationDidStopSelector(#selector(animateDidStop(_:finished:context:)))
// 3.设置目标属性
self.redView.backgroundColor = UIColor.grayColor()
// 4.提交动画
UIView.commitAnimations()
}
func animateDidStart(animationID: NSString, context: UnsafePointer<Void>) {
print("will start")
}
//NSNumber
func animateDidStop(animationID: NSString, finished: Bool, context: UnsafePointer<Void>) {
print(finished)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
编译运行,看到红色方块用5秒变化成了灰色
Paste_Image.png Paste_Image.png