我目前正在尝试创建一个具有基于触摸功能的自定义UIControl。我的问题是,当我调用transform
方法时,opacity
和func setValue forKeyPath
键路径不会被分开,尽管图层动画完全不同。
简单来说:
touchesBegan
和touchesEnded
在opacity
和(b)backgroundCircle
图层上的{a} transform
更改后应用了CATransform3DRotate
} topLayer
。CALayer
的扩展程序(a)通过addAnimation
和(b)添加动画,并通过setValue
设置keyPath的值,从而简化代码。 问题是不透明度不会动画回零,而是逐渐增加,直到完全不透明。但是,当我将keyPath更改为transform
以外的任何内容时,它再次有效但是它不会为topLayer 设置动画。我不确定这里究竟是什么错误,但我怀疑它与keyPaths有关。
以下是方法和扩展:
backgroundCircle
相关功能:
func animateCircleToTransparent(completion:()->()) {
let drawAnimation = CABasicAnimation(keyPath: "opacity")
drawAnimation.duration = 0.2
drawAnimation.repeatCount = 1
drawAnimation.fromValue = backgroundCircleOpacity
drawAnimation.toValue = 0
drawAnimation.fillMode = kCAFillModeBackwards
drawAnimation.autoreverses = false
drawAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.34, 0.01, 0.69, 1.37)
self.backgroundCircle.ocb_applyAnimation(drawAnimation)
}
func animateCircleToOpaque(completion:()->()) {
let drawAnimation = CABasicAnimation(keyPath: "opacity")
drawAnimation.duration = 0.2
drawAnimation.repeatCount = 1
drawAnimation.fromValue = 0
drawAnimation.toValue = backgroundCircleOpacity
drawAnimation.fillMode = kCAFillModeBackwards
drawAnimation.autoreverses = false
drawAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.34, 0.01, 0.69, 1.37)
self.backgroundCircle.ocb_applyAnimation(drawAnimation)
}
topLayer
相关功能:
func animateTopLineOn() {
let topTransform = CABasicAnimation(keyPath: "transform")
topTransform.timingFunction = CAMediaTimingFunction(controlPoints: 0.5, -0.8, 0.5, 1.85)
topTransform.duration = 0.4
topTransform.fillMode = kCAFillModeBackwards
let translation = CATransform3DMakeTranslation(-4, 0, 0)
topTransform.toValue = NSValue(CATransform3D: CATransform3DRotate(translation, -0.7853975, 0, 0, 1))
topTransform.beginTime = CACurrentMediaTime() + 0.25
self.topLayer.ocb_applyAnimation(topTransform)
}
func animateTopLineOff() {
let topTransform = CABasicAnimation(keyPath: "transform")
topTransform.timingFunction = CAMediaTimingFunction(controlPoints: 0.5, -0.8, 0.5, 1.85)
topTransform.duration = 0.4
topTransform.fillMode = kCAFillModeBackwards
let translation = CATransform3DMakeTranslation(4, 0, 0)
topTransform.toValue = NSValue(CATransform3D: CATransform3DRotate(translation, 0.7853975, 0, 0, 1))
topTransform.beginTime = CACurrentMediaTime() + 0.25
self.topLayer.ocb_applyAnimation(topTransform)
}
'的CALayer'扩展:
extension CALayer {
func ocb_applyAnimation(animation: CABasicAnimation) {
let copy = animation.copy() as! CABasicAnimation
if copy.fromValue == nil {
copy.fromValue = self.presentationLayer()!.valueForKeyPath(copy.keyPath!)
}
self.addAnimation(copy, forKey: copy.keyPath!)
self.setValue(copy.toValue, forKeyPath:copy.keyPath!)
}
}