美文网首页demoEdisonSwift 专栏
swift Animation-CABasicAnimation

swift Animation-CABasicAnimation

作者: IIronMan | 来源:发表于2017-06-12 00:13 被阅读244次

CABasicAnimation的一些简单实现的动画,例如移动、透明度、翻转等等。方法里面传入一个CALayer类或者子类就可以了

  • 移动动画position
    func addLayerAnimationPosition(layer: CALayer) {
    let animation = CABasicAnimation(keyPath: "position")
    //开始的位置
    animation.fromValue = NSValue(CGPoint: layer.position)
    //移动到的位置
    animation.toValue = NSValue(CGPoint: CGPointMake(120, 200))
    //持续时间
    animation.duration = 3
    //运动后的位置保持不变(layer的最后位置是toValue)
    animation.removedOnCompletion = false
    animation.fillMode = kCAFillModeForwards

      //添加动画
      layer.addAnimation(animation, forKey: "addLayerAnimationPosition")
     }
    
  • 透明度 opacity
    func addLayerAnimationOpacity(layer: CALayer) {
    let animation = CABasicAnimation(keyPath: "opacity")
    animation.fromValue = 1
    animation.toValue = 0
    animation.duration = 3

       layer.addAnimation(animation, forKey: "addLayerAnimationOpacity")
    
     }
    
  • 变大与位置 bounds
    func addLayerAnimationBounds(layer:CALayer) {
    let animation = CABasicAnimation(keyPath: "bounds")
    animation.toValue = NSValue(CGRect:CGRectMake(130, 200, 140, 140))
    animation.duration = 3
    animation.repeatCount = 1

       layer.addAnimation(animation, forKey: "addLayerAnimationBounds")
     }
    
  • 由小变大 bounds.size
    func addLayerAnimationBoundsSize(layer:CALayer) {
    let animation = CABasicAnimation(keyPath: "bounds.size")
    animation.fromValue = NSValue(CGSize: layer.bounds.size)
    animation.toValue = NSValue(CGSize:CGSizeMake(layer.bounds.size.width+20, layer.bounds.size.height+20))
    animation.duration = 3
    animation.repeatCount = 1

       layer.addAnimation(animation, forKey: "addLayerAnimationBoundsSize")
     }
    
  • 改变颜色 backgroundColor
    func addLayerAnimationBackgroundColor(layer:CALayer) {
    let animation = CABasicAnimation(keyPath: "backgroundColor")
    animation.toValue = UIColor.blueColor().CGColor
    animation.duration = 3
    animation.repeatCount = 1

      layer.addAnimation(animation, forKey: "addLayerAnimationMargin")
    }
    
  • 渐变圆角 cornerRadius
    func addLayerAnimationCornerRadius(layer:CALayer) {
    let animation = CABasicAnimation(keyPath: "cornerRadius")
    animation.toValue = 30
    animation.duration = 3
    animation.repeatCount = 1

       layer.addAnimation(animation, forKey: "addLayerAnimationCornerRadius")
     }
    
  • 改变边框border的大小(图形周围边框,border默认为黑色), borderWidth
    func addLayerAnimationBorderWidth(layer:CALayer) {
    let animation = CABasicAnimation(keyPath: "borderWidth")
    animation.toValue = 10
    animation.duration = 3
    animation.repeatCount = 1

        layer.addAnimation(animation, forKey: "addLayerAnimationBorderWidth")
      }
    
  • 改变layer内容(图片),注意如果想要达到改变内容的动画效果,首先在运行动画之前定义好layer的contents contents
    func addLayerAnimationContents(layer:CALayer) {
    let animation = CABasicAnimation(keyPath: "contents")
    let toImage = UIImage.init(named: "通车辆设计矢量素材-06.png")?.CGImage
    animation.toValue = toImage
    animation.duration = 3
    animation.repeatCount = 1

       layer.addAnimation(animation, forKey: "addLayerAnimationBounds")
     }
    
  • 缩放、放大 transform.scale
    func addLayerAnimationTransformScale(layer:CALayer) {
    let animation = CABasicAnimation(keyPath: "transform.scale")
    //开始时的倍率
    animation.fromValue = 1.0
    //结束时的倍率
    animation.toValue = 0.5
    animation.duration = 3
    animation.repeatCount = 1
    animation.autoreverses = true

       layer.addAnimation(animation, forKey: "addLayerAnimationScale")
      }
    
  • 旋转动画(翻转,沿着X轴) transform.rotation.x
    func addLayerAnimationTranformRotationX(layer: CALayer) {
    let animation = CABasicAnimation(keyPath: "transform.rotation.x")
    //旋转180度 = PI
    animation.toValue = M_PI
    animation.duration = 3
    animation.repeatCount = 1
    //这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

      layer.addAnimation(animation, forKey: "addLayerAnimationTranformRotationX")
    }
    
  • 旋转动画(翻转,沿着Y轴) transform.rotation.y
    func addLayerAnimationTranformRotationY(layer: CALayer) {
    let animation = CABasicAnimation(keyPath: "transform.rotation.y")
    //旋转180度 = PI
    animation.toValue = M_PI
    animation.duration = 3
    animation.repeatCount = 1
    //这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

      layer.addAnimation(animation, forKey: "addLayerAnimationTranformRotationY")
    }
    
  • 旋转动画(沿着Z轴) transform.rotation.z
    func addLayerAnimationTranformRotationZ(layer: CALayer) {
    let animation = CABasicAnimation(keyPath: "transform.rotation.z")
    //旋转360度 = PI2
    animation.toValue = M_PI
    2
    animation.duration = 3
    animation.repeatCount = 1
    //这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

      layer.addAnimation(animation, forKey: "addLayerAnimationTranformRotationZ")
    }
    
  • 横向移动(沿着X轴) transform.translation.x
    func addLayerAnimationTranformTranslationX(layer: CALayer) {
    let animation = CABasicAnimation(keyPath: "transform.translation.x")
    animation.toValue = 20
    animation.duration = 3
    animation.repeatCount = 1
    //这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

      layer.addAnimation(animation, forKey: "addLayerAnimationTranformTranslationX")
    }
    
  • 纵向移动(沿着Y轴) transform.translation.y
    func addLayerAnimationTranformTranslationY(layer: CALayer) {
    let animation = CABasicAnimation(keyPath: "transform.translation.y")
    animation.toValue = 20
    animation.duration = 3
    animation.repeatCount = 1
    //这里我们可以添加可以不添加,添加一个缓慢进出的动画效果(int/out)。当不添加时,匀速运动,会使用kCAMediaTimingFunctionLinear;当添加时,layer会在开始和结束时比较缓慢
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

      layer.addAnimation(animation, forKey: "addLayerAnimationTranformTranslationY")
     }

相关文章

网友评论

    本文标题:swift Animation-CABasicAnimation

    本文链接:https://www.haomeiwen.com/subject/igmkqxtx.html