如 NSAnimationContext
文档中所示,使用 NSAnimationContext.runAnimationGroup(_:_:)
为帧原点 和 大小设置动画预期用于某些 View 类型(包括 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
的最终工作 list 是:
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
的框架使动画起作用?
最佳答案
我怀疑这与运行时生成的隐式自动布局约束有关。修改框架后,自动布局会将其恢复为原始大小。
我放弃了原来的方法,转而采用以下方法:
- 在 Interface Builder 中创建宽度和/或高度约束。我使用默认优先级 1000(约束是强制性的)。
- 为宽度和/或高度
NSLayoutConstraint
创建一个导出。这在 IB 中很棘手:我必须在检查器的测量选项卡中双击约束,然后在检查器中打开约束对象。然后您可以选择连接选项卡并连接 socket 。 - 在我的
NSViewController
子类中,我使用 anchor 来定义新的高度或宽度:theWidthConstraint.constant = 200
后跟self.view.needsUpdateConstraints = true
这种方法更简洁,并且与自动布局系统更兼容。此外,它还可以轻松地为新自动布局产生的整个布局更改设置动画:
NSAnimationContext.runAnimationGroup({(let context) -> Void in
context.duration = 1.0
self.theWidthConstraint.animator().constant = 200
// Other constraint modifications can go here
}) {
print("Animation done")
}
关于swift - NSAnimationContext 不会为 NSButton 设置动画帧大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39618005/