【问题标题】:NSView Animation not animatingNSView 动画没有动画
【发布时间】:2021-07-03 10:07:24
【问题描述】:

我正在尝试通过更改其前导约束的值来为 NSView 设置动画。

这是我的约束对象:

@IBOutlet weak var vModDatesLeading: NSLayoutConstraint!

以及我用来为其位置设置动画的代码:

@IBAction func getModDates(_ sender: Any) {
    
    NSAnimationContext.runAnimationGroup({ context in

        //Indicate the duration of the animation
        context.duration = 0.5
        vModDatesLeading.constant = 0
        vModDatesLeading.animator()
        vModDatesWrapper.layoutSubtreeIfNeeded()
       
       }, completionHandler:nil)
}

它在移动,但在跳跃,而不是动画。如果这是 iOS,我会添加:

view.layoutIfNeeded()

但这不是 NSView 的选项。我试过layoutSubtreeIfNeeded(),但没用。

任何帮助将不胜感激。

【问题讨论】:

标签: swift macos animation nsview


【解决方案1】:

这些天我主要在 iOS 中工作,我认为我从未使用过 NSAnimationContext。我做了一点挖掘,看起来你需要添加行

context.current.allowsImplicitAnimation = true

在你的 runAnimationGroup 闭包体内。

编辑:

我刚刚注意到这条线

vModDatesLeading.animator()

在您的代码中。那没有任何作用。这个想法是,您应该为您正在制作动画的对象获取动画代理,并将您的更改应用于该代理。因此,您的代码应为:

NSAnimationContext.runAnimationGroup({ context in

    //Indicate the duration of the animation
    context.duration = 0.5
    vModDatesLeading.animator().constant = 0
    vModDatesWrapper.layoutSubtreeIfNeeded()
}

(请注意,我没有对此进行测试。我认为不需要context.current.allowsImplicitAnimation = true 就可以工作,但我必须对其进行测试才能确定。)

【讨论】:

  • NSAnimationContext.currentcontext 是同一个上下文吗?
  • 是的。抱歉,我正在处理一个稍微不同的示例,其中未定义 context。我将编辑我的答案。
  • contextNSAnimationContext.current 应该可以工作。
  • 只是想让你知道我已经看过这个但还没有机会测试它。
  • 你需要vModDatesWrapper.layoutSubtreeIfNeeded()吗?还是答案和Animate Auto Layout Constraints and Alpha on NSView一样?
猜你喜欢
  • 2012-07-01
  • 2012-05-08
  • 2017-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-21
  • 2015-12-30
  • 2020-05-19
相关资源
最近更新 更多