IOS-swift 动画02 CAKeyframeAnimation

CAKeyframeAnimation 关键帧动画

关键帧动画 主要用于多步骤动画

主要属性:

  • keyPath : 要设置的属性
  • path : 路径 可用UIBezierPath
  • duration : 动画时长
  • repeatCount : 重复次数
  • calculationMode : 动画计算方式
可选属性说明
kCAAnimationLinear默认差值
kCAAnimationDiscrete逐帧显示
kCAAnimationPaced匀速 无视keyTimes
kCAAnimationCubickeyValue之间曲线平滑 可用tensionValues,continuityValues,biasValues 调整
kCAAnimationCubicPacedkeyValue之间平滑差值 无视keyTimes
  • rotationMode : 旋转方式
可选属性说明
kCAAnimationRotateAuto自动
kCAAnimationRotateAutoReverse自动翻转
  • values : 动画位置数组
  • keyTimes : 动画发生的时间点数组 ( 0 -> 1) 与动画位置对应

上代码:

//
//  ViewController.swift
//  KeyframeAnimation
//
//  Created by 鲍东升 on 16/5/19.
//  Copyright © 2016年 鲍东升. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    //MARK: - PropertyList
    var lineActionView      :   UIView?
    var circleAtcionView    :   UIView?

    private lazy var lineKeyAnimation:CAKeyframeAnimation={
        var val=CAKeyframeAnimation()
        return val
    }()

    private lazy var circleKeyAnimation:CAKeyframeAnimation={
        var val=CAKeyframeAnimation()
        return val
    }()

    //MARK: -
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        prepareView(self.view)
    }

    func prepareView(view:UIView){
        view.backgroundColor    = UIColor.orangeColor()
        lineActionView          = UIView(frame: CGRect(x: 20, y: 100, width: 50, height: 50))
        circleAtcionView        = UIView(frame: CGRect(x: 20, y: 200, width: 80, height: 50))
        lineActionView?.backgroundColor     =   UIColor.cyanColor()
        circleAtcionView?.backgroundColor   =   UIColor.purpleColor()
        view.addSubview(lineActionView!)
        view.addSubview(circleAtcionView!)
    }


    func toolAddAnimation2View(view:UIView,key:String,animation:CAKeyframeAnimation,procFunc:((CAKeyframeAnimation)->(CAKeyframeAnimation))) -> UIView {
        view.layer.addAnimation(procFunc(animation), forKey: key)
        return view
    }


    //MARK: - config

    func configLineKeyAnimation(animation:CAKeyframeAnimation) -> CAKeyframeAnimation {
        animation.keyPath   =   "position.x"
        animation.values    =   [20,120,180,210,300,20]
        animation.keyTimes  =   [0,0.2,0.7,0.8,0.9,1]
        animation.duration  =   3
        //动画开始前设置动画
        animation.additive  =   true
        return animation
    }

    func configCircleKeyAnimation(animation:CAKeyframeAnimation) -> CAKeyframeAnimation {
        animation.keyPath   =   "position"
        animation.path      =   UIBezierPath(ovalInRect: CGRect(x: 20, y: 200, width: 200, height: 200)).CGPath
        animation.duration  =   3
        animation.additive  =   true
        animation.repeatCount   =   3
        /*
         kCAAnimationLinear     :   默认差值
         kCAAnimationDiscrete   :   逐帧显示
         kCAAnimationPaced      :   匀速 无视keyTimes
         kCAAnimationCubic      :   keyValue之间曲线平滑 可用 tensionValues,continuityValues,biasValues 调整
         kCAAnimationCubicPaced :   keyValue之间平滑差值 无视keyTimes
         */
        animation.calculationMode   =   kCAAnimationPaced //无视keytimes
        /*
         kCAAnimationRotateAuto         :   自动
         kCAAnimationRotateAutoReverse  :   自动翻转
         不设置则不旋转
         */
        animation.rotationMode  =   kCAAnimationRotateAuto

        return animation
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        toolAddAnimation2View(lineActionView!, key: "lineKeyAnimation", animation:lineKeyAnimation, procFunc: configLineKeyAnimation)
        toolAddAnimation2View(circleAtcionView!, key: "circlceKeyAnimation", animation:circleKeyAnimation, procFunc: configCircleKeyAnimation)
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值