【发布时间】:2021-02-07 20:49:03
【问题描述】:
Swift 3、iOS 10、macOS 10.12.4
我正在构建一个可在 iOS 和 Mac 上运行的应用程序。在 iOS 端,我成功地为 UIView 制作了动画。当用户点击某物时,会出现一个弹出窗口并动画到位。这是我在 tap 事件中的代码:
self.popupConstraintX.constant = x
self.popupConstraintY.constant = y
UIView.animate(withDuration: 0.25 , delay: 0.0, options: .curveLinear, animations: {
self.graphPopup.alpha = 1.0
self.layoutIfNeeded()
}, completion:nil)
在这种情况下,self 指的是一个拥有graphPopup 的UITableViewCell。
我在 Mac 端构建了相同的东西,但我正在尝试为 graphPopup 制作动画,现在是 NSView。以下是我目前在 click 事件中的内容:
self.popupConstraintX.constant = x
self.popupConstraintY.constant = y
self.view.layoutSubtreeIfNeeded()
NSAnimationContext.runAnimationGroup({_ in
self.graphPopup.alphaValue = 1.0
//Indicate the duration of the animation
NSAnimationContext.current().duration = 0.25
NSAnimationContext.current().allowsImplicitAnimation = true
self.view.updateConstraints()
self.view.layoutSubtreeIfNeeded()
}, completionHandler:nil)
这里的self指的是包含NSViewController。没有任何动画——不是graphPopup 的位置或alpha。它只是出现又消失,就像 1985 年在 Atari 上一样。
知道我的NSView 动画有什么问题吗?
更新
为了后代,这里是 BJ 建议的工作代码(稍作调整以使用隐式动画上下文):
self.popupConstraintX.constant = x
self.popupConstraintY.constant = y
NSAnimationContext.runAnimationGroup({context in
context.duration = 0.25
context.allowsImplicitAnimation = true
self.graphPopup.alphaValue = 1.0
self.view.layoutSubtreeIfNeeded()
}, completionHandler:nil)
【问题讨论】: