今天开始写一下 关于IOS动画的相关内容
正如大家所了解的 IOS中动画是加载在UIVIEW 的layer上的
进而通过Core Graphics 框架 进行渲染 也就是说我们给layer 添加动画时并不是直接对layer进行修改操作的
layer中包含两个 同行的layer层次 分别是
presentationLayer 呈现: 动画进行时的显示状态. presentationLayer 的属性和动画运行过程中界面上看到的是一致的
可用 layer.presentationLayer() 进行获取
modelLayer 模型: 用于显示的 可用 layer.modelLayer() 进行获取
CABasicAnimation 基础动画上代码
//
// ViewController.swift
// AnimationStd
//
// Created by 鲍东升 on 16/5/18.
// Copyright © 2016年 鲍东升. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//MARK: - PropertyList
var playView1:UIView?
var playView2:UIView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
prepareUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func prepareUI(){
playView1 = UIView(frame: CGRect(x: 100, y: 200, width: 40, height: 40))
playView2 = UIView(frame: CGRect(x: 100, y: 300, width: 40, height: 40))
playView1?.backgroundColor = UIColor.grayColor()
playView2?.backgroundColor = UIColor.grayColor()
}
func addLineAnimationFor(view1:UIView,view2:UIView,offset:CGFloat)->(view1:UIView,view2:UIView) {
//支持的 KeyPath
//https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/Key-ValueCodingExtensions/Key-ValueCodingExtensions.html
let centerX:CGFloat = view1.center.x
let lineAnimation:CABasicAnimation = CABasicAnimation(keyPath: "position.x")
/*
* 设置差值 :fromValue toValue byValue 最多设置两项
* 不同组合所生成的效果也不同
* 当 fromValue 和 toValue 都不为空. 从 fromValue 到 toValue 进行差值.
* 当 fromValue 和 byValue 都不为空. 从 fromValue 到 (fromValue + byValue) 进行差值.
* 当 byValue 和 toValue 都不为空. 从 (toValue - byValue) 到 toValue 进行差值.
* 当 fromValue 不为空. 从fromValue 到 当前显示的属性值 进行差值.
* 当 toValue 不为空. 从presentation layer(展示的layer)的keyPath 的值 到 toValue 进行差值.
* 当 byValue 不为空. 从presentation layer(展示的layer)的keyPath 的值 到 这个值和byValue之和 进行差值.
* 当 全部为空. 从presentation layer(展示的layer)的keyPath 的前一个值 到 presentation layer(展示的layer)的keyPath 的当前值进行差值.
*/
//from -> to 方式
lineAnimation.fromValue = centerX
lineAnimation.toValue = centerX+offset
//byValue 方式
// lineAnimation.byValue = offset
lineAnimation.duration = 1//second
//save last position
//type1: // 带来更多内存开销 有额外绘画
//fillMode :
/*
* kCAFillModeRemoved 默认 动画结束后恢复至之前状态
* kCAFillModeForwards . 动画结束后保持最后状态
* kCAFillModeBackwards . 动画开始前显示开始状态(frame)
* kCAFillModeBoth . 兼顾kCAFillModeForwards和kCAFillModeBackwards
*/
lineAnimation.fillMode = kCAFillModeForwards
lineAnimation.removedOnCompletion = false
//type2:
// view.center = CGPoint(x: view.center.x+offset, y: view.center.y)
view1.layer.addAnimation(lineAnimation, forKey: "lineAnimation")
//延迟0.5秒 由于动画添加的时候会被复制 所以VIEW1的动画不受影响
// beginTime 相对于父级对象的开始时间 默认为0
lineAnimation.beginTime = CACurrentMediaTime()+0.5
view2.layer.addAnimation(lineAnimation, forKey: "lineAnimation")
return (view1,view2)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let result = addLineAnimationFor(playView1!,view2: playView2!,offset: 100)
view.addSubview(result.view1)
view.addSubview(result.view2)
}
}
参考:https://www.objc.io/issues/12-animations/animations-explained/