今天好好整理下ios中用到的动画
首先是UIViewAnimation动画
首先是最传统的方法 也是苹果建议的方法
UIView.animateWithDuration(2.0, delay: 0.0, options:[UIViewAnimationOptions.CurveEaseIn,UIViewAnimationOptions.Repeat], animations: { () -> Void in self.firstView?.frame.origin.x += 100 }, completion: nil)
begin和commit的方法
func beginCommit() { //开始动画 UIView.beginAnimations("animate", context: nil) //设置动画的一些参数 UIView.setAnimationDuration(2.0) UIView.setAnimationRepeatAutoreverses(true) //设置代理 UIView.setAnimationDelegate(self) self.firstView?.frame.origin.x += 100 //代理的两个函数 UIView.setAnimationWillStartSelector("animationDidStart:") UIView.setAnimationDidStopSelector("animationDidStop:") //结束动画 UIView.commitAnimations() } override func animationDidStart(anim: CAAnimation) { print("1") } override func animationDidStop(anim: CAAnimation, finished flag: Bool) { print("2") }内嵌nest的方法 默认继承外部的option和duration 不过可以改变 重写即可
UIView.animateWithDuration(2.0, delay: 0.0, options:[UIViewAnimationOptions.CurveEaseIn,UIViewAnimationOptions.Repeat], animations: { () -> Void in self.firstView?.frame.origin.x += 100 UIView.animateWithDuration(3.0, delay: 0.0, options: [UIViewAnimationOptions.OverrideInheritedDuration,UIViewAnimationOptions.OverrideInheritedCurve,UIViewAnimationOptions.CurveEaseIn], animations: { () -> Void in self.secondView?.frame.origin.x += 100 }, completion: nil) }, completion: nil)替换view和转场View
//代替view func replaceView() { let view1 = UIView(frame: CGRectMake(89,90,500,500)) view.backgroundColor = UIColor.greenColor() //代替上面的View 防止动画发生在层上面 用allowContent UIView.transitionFromView(self.secondView!, toView: view1, duration: 2.0, options: [UIViewAnimationOptions.AllowAnimatedContent,UIViewAnimationOptions.CurveEaseOut], completion: nil) } }
UIView.transitionWithView(<#T##view: UIView##UIView#>, duration: <#T##NSTimeInterval#>, options: <#T##UIViewAnimationOptions#>, animations: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>, completion: <#T##((Bool) -> Void)?##((Bool) -> Void)?##(Bool) -> Void#>)
//创建layer var layer = CALayer() //根layer也就是view的layer self.view.layer layer.backgroundColor = UIColor.magentaColor().CGColor //layer的frame layer.frame = CGRectMake(100, 100, 100, 100) layer.shadowOffset = CGSize(width: 5.0, height: 5.0) //阴影的透明度 layer.shadowOpacity = 5.0 self.view.layer.addSublayer(layer)
self.layer.position.x += 100
func basicAnimate() { //这里是设置哪个path 是定死的 具体可查表 let basic = CABasicAnimation(keyPath: "tranform.scale") //设定动画的内容 初始值 最终值 加的值 basic.fromValue = 1.0 basic.toValue = 2.0 basic.byValue = 0.2 //设置时间 basic.duration = 3.0 //设置循环 还要回去 basic.autoreverses = true //设置循环的次数 因为是浮点数 所以赋值最大的浮点数 basic.repeatCount = MAXFLOAT //设置动画的交代对象 还有标示这个动画 mainView.layer.addAnimation(basic, forKey: "key") }
关键帧动画 设定每一帧的位置 比如 随后可以设定每一帧之间执行的时间速度和长短等
func keyFrame() { //用关键帧动画 let layer = self.image?.layer let keyAnimate = CAKeyframeAnimation(keyPath: "position") //设置每一帧 let pos = layer?.position let value0 = NSValue(CGPoint: pos!) let value1 = NSValue(CGPoint: CGPointMake((pos?.x)!, pos!.y + 200)) let value2 = NSValue(CGPoint: CGPointMake((pos?.x)! - 200, pos!.y + 200)) let value3 = NSValue(CGPoint: CGPointMake((pos?.x)! - 200, pos!.y)) //回到最终值 let value4 = NSValue(CGPoint: CGPointMake((pos?.x)!, pos!.y )) keyAnimate.values = [value0,value1,value2,value3,value4] //设置每一帧动画间执行速度的快慢 let tf0 = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn) let tf1 = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) let tf2 = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut) let tf3 = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) //let tf4 = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault) keyAnimate.timingFunctions = [tf0,tf1,tf2,tf3] //设置每一帧之间的时间 在每一帧停留的时间 keyAnimate.duration = 12 keyAnimate.autoreverses = false keyAnimate.keyTimes = [0.5,1.0,2.0,3.0,1.0] self.image?.layer.addAnimation(keyAnimate, forKey: "animate") }