gpt4 book ai didi

swift - CABasicAnimation 忽略持续时间

转载 作者:行者123 更新时间:2023-11-30 10:11:36 26 4
gpt4 key购买 nike

无论我是否使用 CATransation,我似乎都无法控制简单核心动画的持续时间。

事务完成代码在我的持续时间设置后触发,就像我期望的那样,但旋转动画总是需要大约 0.5 秒,无论它设置为什么持续时间。

但是,如果我注释掉设置变换的最后一行,动画就会以适当的速度移动,但随后会恢复到原始旋转。

我错过了什么?

class MyControl: NSControl {
var shapeLayer = CAShapeLayer()

required override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.wantsLayer = true
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay

// create a triangle shape
let path = NSBezierPath()
path.moveToPoint(CGPointMake(45.0, 25.0))
path.lineToPoint(CGPointMake(5.0, 5.0))
path.lineToPoint(CGPointMake(5.0, 45.0))
path.lineToPoint(CGPointMake(45.0, 25.0))
path.closePath()
shapeLayer.frame = self.bounds
shapeLayer.path = path.toCGPath()
shapeLayer.fillColor = NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0).CGColor
self.layer!.addSublayer(shapeLayer)
}

override func mouseUp(theEvent: NSEvent){
let duration = 2.0
let endAngle = -90.0
let startAngle = shapeLayer.valueForKey("transform.rotation")

CATransaction.begin()
CATransaction.setAnimationDuration(duration)
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))
CATransaction.setCompletionBlock({
Swift.print("this fires after 2 seconds")
})

let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.removedOnCompletion = true
rotateAnimation.autoreverses = false
rotateAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
rotateAnimation.duration = duration
rotateAnimation.fromValue = startAngle
rotateAnimation.toValue = endAngle.degreesToRadians
shapeLayer.addAnimation(rotateAnimation, forKey: rotateAnimation.keyPath)
CATransaction.commit()

// mystery line...
shapeLayer.transform = CATransform3DMakeRotation(endAngle.degreesToRadians, 0.0, 0.0, 1.0)
}


}

顺便说一句,我使用了几个有用的扩展...NSBezierPath 中的 toCGPath 和 CGFloat 中的 DegreeToRadians。

最佳答案

您看到这种奇怪行为的原因是使用了显式 CATransaction begin--commit。这将依次在运行循环已拥有的 CATransaction 中添加一个嵌套的 CATransaction。所以发生的情况是,您的内部事务被提交,但在关联的动画完成之前,您立即获得了新的转换值。这将导致这个故障,您的图层将快速旋转并消失。

取而代之的是,直接的方法是1. 创建动画2. 为您的动画设置委托(delegate)3. 在委托(delegate)回调中设置您希望图层保留的目标值。

 override func mouseUp(theEvent: NSEvent){
let duration = 2.0
let endAngle = -90.0
let startAngle = shapeLayer.valueForKey("transform.rotation")

//Set up animation
let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.removedOnCompletion = true
rotateAnimation.autoreverses = false
rotateAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
rotateAnimation.duration = duration
rotateAnimation.fromValue = startAngle
rotateAnimation.toValue = endAngle * 3.142/180

//Setup delegate
rotateAnimation.delegate = self

shapeLayer.addAnimation(rotateAnimation, forKey: @"rotationAnimation")

}


public override func animationDidStop(anim: CAAnimation, finished flag: Bool){
shapeLayer.transform = CATransform3DMakeRotation(CGFloat(-90.0 * 3.142/180), 0.0, 0.0, 1.0)
}

P.S:我还没有运行此代码,并请注意动画完成后您要为图层的变换设置什么值。

关于swift - CABasicAnimation 忽略持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32680621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com