首页 > 系统 > iOS > 正文

IOS swift中的动画的实例详解

2020-07-26 02:42:09
字体:
来源:转载
供稿:网友

IOS swift中的动画的实例详解

UIView的通用动画

let view = UIView(frame: CGRectMake(10.0, 10.0, 100.0, 40.0))self.view.addSubview(view)view.backgroundColor = UIColor.lightGrayColor()// 位置改变var frame = view.frameUIView.animateWithDuration(0.6, delay: 2.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {      () -> Void in      frame.origin.x = 200.0      view.frame = frame      }) {        (finished:Bool) -> Void in        UIView.animateWithDuration(0.6) {          () -> Void in          frame.origin.x = 10.0          view.frame = frame        }}

CABasicAnimation核心动画

1、CABasicAnimation类只有三个属性:

fromValue:开始值 toValue:结束值 Duration:动画的时间 repeatCount:重复次数

2、通过animationWithKeyPath键值对的方式设置不同的动画效果

transform.scale transform.scale.x transform.scale.y transform.rotation.z opacity margin zPosition backgroundColor cornerRadius borderWidth bounds contents contentsRect cornerRadius frame hidden mask masksToBounds opacity position shadowColor shadowOffset shadowOpacity shadowRadius
let view = UILabel(frame: CGRectMake((self.view.frame.size.width - 200.0) / 2, 10.0, 200.0, 40.0))self.view.addSubview(view)view.text = "缩放/淡入淡出"view.textAlignment = .Centerview.adjustsFontSizeToFitWidth = trueview.backgroundColor = UIColor.lightGrayColor()//let layer = view.layer// 开始动画// 缩放let scaleAnimate = CABasicAnimation(keyPath: "transform.scale")scaleAnimate.fromValue = 1.0scaleAnimate.toValue = 1.5scaleAnimate.autoreverses = truescaleAnimate.repeatCount = MAXFLOATscaleAnimate.duration = 1.0// 淡入淡出let opaqueAnimate = CABasicAnimation(keyPath: "opacity")opaqueAnimate.fromValue = 0.1opaqueAnimate.toValue = 1opaqueAnimate.autoreverses = trueopaqueAnimate.repeatCount = MAXFLOATopaqueAnimate.duration = 1.0layer.addAnimation(scaleAnimate, forKey: "scaleAnimate")layer.addAnimation(opaqueAnimate, forKey: "opacityAnimate")
// 组合动画let view3 = UILabel(frame: CGRectMake(10.0, (currentView.frame.origin.y + currentView.frame.size.height + 10.0), 120.0, 40.0))self.view.addSubview(view3)view3.text = "组合动画"view3.textAlignment = .Centerview3.adjustsFontSizeToFitWidth = trueview3.backgroundColor = UIColor.lightGrayColor()//let layer3 = view3.layer// CAAnimationGroup组合动画效果let rotate: CABasicAnimation = CABasicAnimation()rotate.keyPath = "tranform.rotation"rotate.toValue = M_PIlet scale: CABasicAnimation = CABasicAnimation()scale.keyPath = "transform.scale"scale.toValue = 0.0let move: CABasicAnimation = CABasicAnimation()move.keyPath = "transform.translation"move.toValue = NSValue(CGPoint: CGPoint(x: 217, y: 230))let animationGroup:CAAnimationGroup = CAAnimationGroup()animationGroup.animations = [rotate, scale, move]animationGroup.duration = 2.0animationGroup.fillMode = kCAFillModeForwardsanimationGroup.removedOnCompletion = falseanimationGroup.repeatCount = MAXFLOAT//layer3.addAnimation(animationGroup, forKey: nil)

CAKeyframeAnimation关键帧动画

主要属性:

keyPath : 要设置的属性 path : 路径 可用UIBezierPath(设置了path,将忽略values) duration : 动画时长 repeatCount : 重复次数 calculationMode : 动画计算方式 values:每一个关键帧(设置了path,将忽略values) removedOnCompletion:执行完之后不删除动画 fillMode:执行完之后保存最新的状态 delegate:代理
let view = UILabel(frame: CGRectMake((self.view.frame.size.width - 200.0) / 2, 10.0, 200.0, 40.0))self.view.addSubview(view)view.text = "CAKeyframeAnimation动画"view.backgroundColor = UIColor.lightGrayColor()//let layer = view.layer// 位移let keyAnimate = CAKeyframeAnimation(keyPath: "position")// 设定关键帧let value0 = NSValue(CGPoint: layer.position)let value1 = NSValue(CGPoint: CGPointMake(layer.position.x, layer.position.y + 200))let value2 = NSValue(CGPoint: CGPointMake(layer.position.x - 150, layer.position.y + 200))let value3 = NSValue(CGPoint: CGPointMake(layer.position.x - 150, layer.position.y))let value4 = NSValue(CGPoint: layer.position)// 速度曲线let tf0 = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)let tf1 = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)let tf2 = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)let tf3 = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)keyAnimate.timingFunctions = [tf0, tf1, tf2, tf3]// 每段执行的时间keyAnimate.keyTimes = [0.0, 0.5, 0.6, 0.7, 1]//keyAnimate.values = [value0, value1, value2, value3, value4]keyAnimate.autoreverses = falsekeyAnimate.repeatCount = 3keyAnimate.duration = 6.0//keyAnimate.delegate = self//layer.addAnimation(keyAnimate, forKey: "position")
// 代理方法override func animationDidStart(anim: CAAnimation) {    print("开始")}override func animationDidStop(anim: CAAnimation, finished flag: Bool) {    print("结束")}
let view3 = UILabel(frame: CGRectMake(10.0, (currentView.frame.origin.y + currentView.frame.size.height + 10.0), 60.0, 60.0))self.view.addSubview(view3)view3.text = "抖动"view3.backgroundColor = UIColor.lightGrayColor()//let layer3 = view3.layer// 抖动let animation3 = CAKeyframeAnimation()animation3.keyPath = "transform.rotation"// (-M_PI_4 /90.0 * 5)表示-5度 。let value31 = NSValue(CGPoint: CGPointMake(CGFloat(-M_PI_4 / 90.0 * 5.0), 0.0))let value32 = NSValue(CGPoint: CGPointMake(CGFloat(M_PI_4 / 90.0 * 5.0), 0.0))let value33 = NSValue(CGPoint: CGPointMake(CGFloat(-M_PI_4 / 90.0 * 5.0), 0.0))animation3.values = [value31, value32, value33];animation3.removedOnCompletion = falseanimation3.fillMode = kCAFillModeForwardsanimation3.duration = 0.2animation3.repeatCount = MAXFLOAT//layer3.addAnimation(animation3, forKey: nil)

 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表