Mac OS - swift - CABasicAnimation 当前值
我有一个 CABasicAnimation 移动 NSHostingView (它是一个滚动字幕文本)。当用户将鼠标光标放在视图上时,动画必须停止并且可以与视图交互。
我尝试了以下方法:
方法1:
根据Apple文档的建议(https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html#//apple_ref/doc/uid/TP40004514-CH8-SW14),我尝试使用 ConvertTime / speed = 0 来实现暂停/恢复方法。
它有效,动画暂停并恢复,但是当动画暂停时,视图内用户 clic 的坐标是模型层的坐标,而不是表示层的坐标。因此,如果没有动画,点击会响应用户点击的位置。
方法2:
我在我的CABasicAnimation
上尝试了各种动画标志,如下所示:
let animation = CABasicAnimation(keyPath: "position");
animation.duration = 10
animation.fromValue = [0, self.frame.origin.y]
animation.toValue = [-500, self.frame.origin.y]
animation.isCumulative = true
animation.isRemovedOnCompletion = false
animation.fillMode = .forwards
无论我使用什么,当动画被删除时,视图都会回到原来的位置(它跳回原来的位置)。
方法3:
我尝试在删除之前通过读取表示层位置来保存当前动画值,如下所示:
print("before")
let currentPosition = layer.presentation()?.position
print("after")
但此方法永远不会返回! (即“之后”永远不会被打印)。这可能是一个错误还是什么?
我现在没主意了。如果有人有建议,那会有很大帮助。谢谢!
I have a CABasicAnimation moving a NSHostingView (it's a scrolling marquee text). When the user get his mouse cursor over the view, the animation must stops and it can interact with the view.
I tried the following approaches:
Method 1:
As recommended by Apple documentation (https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/AdvancedAnimationTricks/AdvancedAnimationTricks.html#//apple_ref/doc/uid/TP40004514-CH8-SW14), I tried to implement the pause/resume method using the convertTime / speed = 0.
It works, the animation is paused and resumed, but while the animation is paused, the coordinates of the user clic inside the view are the one of the model layer and not the one of the presentation layer. So the click respond to where the user would have click if their was no animation.
Method 2:
I tried the various animation flags on my CABasicAnimation
as follow :
let animation = CABasicAnimation(keyPath: "position");
animation.duration = 10
animation.fromValue = [0, self.frame.origin.y]
animation.toValue = [-500, self.frame.origin.y]
animation.isCumulative = true
animation.isRemovedOnCompletion = false
animation.fillMode = .forwards
Not matter what I use, when the animation is removed, the view get back to it's original position (it jumps back to it's original position).
Method 3:
I tried to save the current animation value before removing by reading the presentation layer position as follow:
print("before")
let currentPosition = layer.presentation()?.position
print("after")
But this method never returns! (ie. "after" is never printed). Could it be a bug or something?
I'm out of idea now. If anyone has a suggestion, it would help a lot. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
访问
layer?.presentation()?.position
不起作用,因为animation.fromValue
和animation.toValue
应该是CGPoint< /code> value:
那么方法 3 就可以正常工作了。
Accessing
layer?.presentation()?.position
doesn't work becauseanimation.fromValue
andanimation.toValue
should beCGPoint
values:Then Method 3 works just fine.