码迷,mamicode.com
首页 > 移动开发 > 详细

IOS Animation-CABasicAnimation例子(简单动画实现)

时间:2016-04-10 14:08:20      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

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

 

下面是用swift实现的,这些方法我们也可以用作公共类来调用:

    //移动动画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度 = PI*2
        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")
    }

 

 

如果我们只要看keypath的值,我们可以看到下面文章

http://www.cnblogs.com/alunchen/p/5300075.html

IOS Animation-CABasicAnimation例子(简单动画实现)

标签:

原文地址:http://www.cnblogs.com/alunchen/p/5373820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!