【发布时间】:2016-09-21 13:29:43
【问题描述】:
如NSAnimationContext 文档中所示,使用NSAnimationContext.runAnimationGroup(_:_:) 对帧原点和 大小进行动画处理对于某些视图类型(包括NSImageView)都可以正常工作。但是,对于 NSButton,它不会按预期工作,除非我在 动画之后添加显式帧大小更改
NSImageView 的动画帧大小
对于 NSImageView,以下内容按预期工作。它被移动到原点,并调整为 200x200:
NSAnimationContext.runAnimationGroup({(let context) -> Void in
context.duration = 2.0
// Get the animator for an NSImageView
let a = self.theImage.animator()
// Move and resize the NSImageView
a.frame = NSRect(x: 0, y: 0, width: 200, height: 200)
}) {
print("Animation done")
}
NSButton 的动画帧大小
当使用 NSButton 执行相同操作时,按钮会移动但不会调整大小:
NSAnimationContext.runAnimationGroup({(let context) -> Void in
context.duration = 2.0
// Get the animator for an NSButton
let a = self.button.animator()
// Move and resize the NSImageView
a.frame = NSRect(x: 0, y: 0, width: 200, height: 200)
}) {
print("Animation done")
}
但是,如果我将以下代码行添加到最后,在所有动画代码之后,它会按预期工作!
self.button.frame = NSRect(x: 0, y: 0, width: 200, height: 200)
NSButton 的最终工作清单是:
NSAnimationContext.runAnimationGroup({(let context) -> Void in
context.duration = 2.0
// Get the animator for an NSButton
let a = self.button.animator()
// Move and resize the NSImageView
a.frame = NSRect(x: 0, y: 0, width: 200, height: 200)
}) {
print("Animation done")
}
self.button.frame = NSRect(x: 0, y: 0, width: 200, height: 200)
我不是在这里寻找礼物马,但我不明白为什么 NSButton 需要这样做,甚至是什么使它起作用。谁能解释为什么在动画代码之后显式设置NSButton的框架使动画工作?
【问题讨论】:
标签: swift macos cocoa animation nsanimationcontext