斯威夫特 3、iOS 10、macOS 10.12.4
我正在构建一个在 iOS 和 Mac 上运行的应用程序。在 iOS 端,我已经成功地为一个UIView
. 当用户点击某物时,会出现一个弹出窗口并动画到位。这是我在点击事件中的代码:
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
指的UITableViewCell
是持有graphPopup
.
我在 Mac 端构建了相同的东西,但我正在尝试graphPopup
为现在的NSView
. 这是我到目前为止在点击事件中的内容:
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
. 没有任何东西可以激活-不是位置或alpha
. graphPopup
它只是出现又消失,就像 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)