NSView 动画没有动画

NSView Animation not animating

我正在尝试通过更改其前导约束的值来为 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(),但那是 nada。

如有任何帮助,我们将不胜感激。

这些天我主要在 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 就可以工作,但我必须测试它才能确定.)