代码之家  ›  专栏  ›  技术社区  ›  Vincent

Mac OS-swift-Cabasicanization当前值

  •  0
  • Vincent  · 技术社区  · 3 年前

    我有一个移动NSHostingView的Cabasicanization(滚动字幕文本)。当用户将鼠标光标移到视图上时,动画必须停止,并且可以与视图交互。

    我尝试了以下方法:

    方法1:
    根据苹果文档的建议( 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")
    

    但这种方法永远不会回来!(即“之后”从不打印)。可能是虫子什么的吗?

    我现在不知道了。如果有人有建议,那会很有帮助。谢谢

    0 回复  |  直到 3 年前
        1
  •  1
  •   McKinley botenvouwer    3 年前

    访问 layer?.presentation()?.position 不起作用是因为 animation.fromValue animation.toValue 应该是 CGPoint 价值观:

    animation.fromValue = CGPoint(x: 0, y: self.frame.origin.y)
    animation.toValue = CGPoint(x: -500, y: self.frame.origin.y)
    

    那么方法3就可以了。