IOS-swift 动画02 CAKeyframeAnimation
2016-05-19 19:51
405 查看
CAKeyframeAnimation 关键帧动画
关键帧动画 主要用于多步骤动画主要属性:
keyPath : 要设置的属性
path : 路径 可用UIBezierPath
duration : 动画时长
repeatCount : 重复次数
calculationMode : 动画计算方式
可选属性 | 说明 |
---|---|
kCAAnimationLinear | 默认差值 |
kCAAnimationDiscrete | 逐帧显示 |
kCAAnimationPaced | 匀速 无视keyTimes |
kCAAnimationCubic | keyValue之间曲线平滑 可用tensionValues,continuityValues,biasValues 调整 |
kCAAnimationCubicPaced | keyValue之间平滑差值 无视keyTimes |
可选属性 | 说明 |
---|---|
kCAAnimationRotateAuto | 自动 |
kCAAnimationRotateAutoReverse | 自动翻转 |
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) } }
相关文章推荐
- 菜鸟成长记-初探Swift
- swift -  构成单例
- oc与swift混编
- swift学习之路(三)浮点型
- swift学习之路(二)整型
- swift Image加载
- Swift AFNetworking 使用实例
- Swift学习第五枪
- swift 2.2 语法 -第一讲
- swift-字符和字符串
- swift-可选值
- swift-元组
- swift-分支
- swift-运算符
- swift 数组、字典、集合、结构体、枚举——swift学习(四)
- Swift-常量变量
- Swift开发:VVDocumenter-Xcode-master插件不能用 --- 已经解决
- Swift (if while)
- 用RxSwift实现一个UITableView
- Swift与OC之间的选择