CAKeyframeAnimation 关键帧动画
关键帧动画 主要用于多步骤动画
主要属性:
- keyPath : 要设置的属性
- path : 路径 可用UIBezierPath
- duration : 动画时长
- repeatCount : 重复次数
- calculationMode : 动画计算方式
可选属性 | 说明 |
---|---|
kCAAnimationLinear | 默认差值 |
kCAAnimationDiscrete | 逐帧显示 |
kCAAnimationPaced | 匀速 无视keyTimes |
kCAAnimationCubic | keyValue之间曲线平滑 可用tensionValues,continuityValues,biasValues 调整 |
kCAAnimationCubicPaced | keyValue之间平滑差值 无视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)
}
}