iOS动画分为显示动画(keyFrameAnimation PropertyAnimation CATransaction)隐式动画 CATransaction表示一个动画事务,可以设置layer层的属性,再结合CAAnimation即可实现动画效果
class func textLayerAnimation(layer: CALayer, durationTime: TimeInterval, delay: TimeInterval, effectAnimationClosure: effectAnimationLayerClosure?,finishedClosure: completionClosure?) -> Void{
let animationObjct = TELayerAnimation()
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
let oldLayer = animationObjct.animatableLayerCopy(layer: layer)
var newLayer: CALayer?
var animationGroup: CAAnimationGroup?
animationObjct.completionBlock = finishedClosure
if let effectAnimationClosure = effectAnimationClosure{
CATransaction.begin()
//关闭隐式动画
CATransaction.setDisableActions(true)
newLayer = effectAnimationClosure(layer)
CATransaction.commit()
}
animationGroup = animationObjct.groupAnimationWithLayerChanges(old: oldLayer, new: newLayer!)
if let textAnimationGroup = animationGroup{
animationObjct.textLayer = layer
textAnimationGroup.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseInEaseOut)
textAnimationGroup.beginTime = CACurrentMediaTime()
textAnimationGroup.duration = durationTime
textAnimationGroup.delegate = animationObjct
layer.add(textAnimationGroup, forKey: textAnimationGroupKey)
}else{
if finishedClosure != nil{
finishedClosure!(true)
}
}
}
}