Swift 卡巴斯基化反向(向后)

Swift 卡巴斯基化反向(向后),swift,swift2,core-animation,cabasicanimation,Swift,Swift2,Core Animation,Cabasicanimation,我有点挣扎与这个简单的线动画。我知道如何暂停,但我需要的是能够从调用函数resetAnimation()的那一刻起将动画反转回起始点 let path animation=cabasicanition(关键路径:“strokeEnd”) 让pathLayer=CAShapeLayer() func线性化(){ let path=UIBezierPath() 让screenWidth=self.view.bounds.width 让屏幕高度=self.view.bounds.height path

我有点挣扎与这个简单的线动画。我知道如何暂停,但我需要的是能够从调用函数resetAnimation()的那一刻起将动画反转回起始点

let path animation=cabasicanition(关键路径:“strokeEnd”)
让pathLayer=CAShapeLayer()
func线性化(){
let path=UIBezierPath()
让screenWidth=self.view.bounds.width
让屏幕高度=self.view.bounds.height
path.moveToPoint(CGPointMake(屏幕宽度、屏幕高度/2))
path.addLineToPoint(CGPointMake(屏幕宽度-屏幕宽度,屏幕高度/2))
self.pathLayer.frame=self.view.bounds
self.pathLayer.path=path.CGPath
self.pathLayer.strokeColor=UIColor.whiteColor().CGColor
self.pathLayer.fillColor=nil
self.pathLayer.lineWidth=3.0
self.pathLayer.lineCap=kcalinecapound
self.pathLayer.speed=1
self.view.layer.addSublayer(路径层)
self.pathAnimation.duration=5.0
self.pathAnimation.fromValue=0.0
self.pathAnimation.toValue=1.0
addAnimation(路径动画,forKey:“动画”)
}
函数pauseAnimation(){
让pausedTime=pathLayer.convertTime(CACurrentMediaTime(),fromLayer:nil)
pathLayer.speed=0
pathLayer.timeOffset=pausedTime
}
函数重置动画(){
}

您只需创建一个新动画并删除旧动画即可。新动画的起点将是表示层中该特性的当前值。我还建议您将形状层的框架设置为实际贝塞尔形状的边界,而不是整个视图-当您开始四处移动、缩放/旋转等时,这是一个好习惯。否则您将面临一系列时髦的转换或锚定点更改

下面是我要做的:

let pathLayer = CAShapeLayer()

// first, separate your drawing code from your animation code.
// this way you can call animations without instantiating new objects
func drawLine() {
    let path = UIBezierPath()
    // draw your path with no position translation.. move the layer
    path.moveToPoint(CGPointMake(view.bounds.width, 0))
    path.addLineToPoint(CGPointMake(0, 0))
    pathLayer.frame = path.bounds
    // this line sets the position of the layer appropriately
    pathLayer.position = view.bounds.width - pathLayer.bounds.width / 2
    pathLayer.path = path.CGPath
    pathLayer.strokeColor = UIColor.whiteColor().CGColor
    pathLayer.fillColor = nil
    pathLayer.lineWidth = 3.0
    pathLayer.lineCap = kCALineCapRound
    view.layer.addSublayer(pathLayer)
}

func lineAnimation() {
    let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
    pathAnimation.duration = 5.0
    pathAnimation.fromValue = 0.0
    pathAnimation.toValue = 1.0
    pathLayer.addAnimation(pathAnimation, forKey: "strokeEnd")

}

func reverseAnimation() {
    let revAnimation = CABasicAnimation(keyPath: "strokeEnd")
    revAnimation.duration = 5.0
    // every Core Animation has a 'presentation layer' that contains the animated changes
    revAnimation.fromValue = pathLayer.presentationLayer()?.strokeEnd
    revAnimation.toValue = 0.0
    pathLayer.removeAllAnimations()
    pathLayer.addAnimation(revAnimation, forKey: "strokeEnd")
}

请记住,您还需要设置属性,以便保留动画的结束值。

您还可以使用
CAMediaTiming
协议的
自动反转
属性,通过
cabasicaniation

以特定速度倒车,或仅在on go?中完全重置回初始设置以特定速度倒车Swift 4.1的API更新:revAnimation.fromValue=pathLayer.presentation()?.strokeEnd