iOS View Animations and Layer Animations

一. View Animations 一些属性介绍
  UIView.animateWithDuration(0.5, delay: 0.3, options: [], animations: {
      //code 
      username.center.x += self.view.bounds.width                             
}, completion:nil)  在//code 加你想要怎么移动  上面例子可以使username.center.x 向右移动
  • duration: 动画运行时间
  • delay: 在开始动画之前延迟的时间
  • options: 一个集合定义动画 是一个Struct
    UIViewAnimationOptions 继承了 OptionSetType
    .Repeat: 让动画一直循环
    .Autoreverse: 让动画原路返回 和.Repeat 一直可以实现反复播放动画如下面这样
    username.center.x -= view.bounds.width
    UIView.animateWithDuration(0.5, delay: 0.3, options: [.Repeat, .Autoreverse], animations: {
    username.center.x += self.view.bounds.width
    }, completion:nil)
    .Linear: 整个动画过程中平顺没有加速
    .CurveEaseOut:整个动画过程在最后减速
    .CurveEaseIn:整个动画过程在开始先减速
    .CurveEaseInOut: 整个动画过程在开始和结束的时候减速 这也是默认的属性
二. Layer Animations

layer animations 比 View Animations 有更多的动画属性 更加灵活的控制动画

     let flyRight = CABasicAnimation(keyPath: "position.x")
     flyRight.fromValue = -view.bounds.size.width/2
     flyRight.toValue = view.bounds.size.width/2
     flyRight.duration = 0.5                                                          
     backgroundColor.autoreverses = true
     backgroundColor.repeatCount = 10
     username.addAnimation(flyRight, forKey:nil) 

这是简单的Layer Animations 和 UIView Animations 对比 实现的效果和上面的UIViewAnimation 一样 但是Layer Animation 的属性更多 并且可以被复用 下面介绍layer animation 常用属性

  • beginTime
    layer animation 的beginTime 这个属性是动画开始的绝对时间 下面代码可以让动画延迟0.3秒开始
    flyRight.beginTime = CACurrentMediaTime() + 0.3
  • fillMode
    KCAFillModeRemoved 这是默认属性 动画结束后layer恢复之前的状态
    KCAFillModeBackwards 动画开始之前先初始动画 结束之后恢复之前状态
    KCAFillModeForwards 动画结束后变成动画之后的状态
    KCAFillModeBoth 是 KCAFillModeForwards 和 KCAFillModeBackwards的 合并状态
    同时必须设置
    removedOnCompletion = false 不然动画一完成就会消失 结合 fillMode 可以保持动画结束之后不消失
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 先看看CAAnimation动画的继承结构 CAAnimation{ CAPropertyAnimation { ...
    时间不会倒着走阅读 1,694评论 0 1
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,572评论 6 30
  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 3,069评论 0 21
  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 3,169评论 1 23
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,143评论 5 13