IOS Animation-CABasicAnimation例子(簡單動畫實現)

   這些例子都是CABasicAnimation的一些簡單實現的動畫,例如移動、透明度、翻轉等等。方法裏面傳入一個CALayer類或者子類就能夠了。html

 

下面是用swift實現的,這些方法咱們也能夠用做公共類來調用: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.htmlspa

 

 

能夠關注本人的公衆號,多年經驗的原創文章共享給你們。設計

相關文章
相關標籤/搜索