NSAnimationContext 不会为 NSButton 设置动画帧大小

NSAnimationContext does not animate frame size for NSButton

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 的帧使动画起作用?

我怀疑这与运行时生成的隐式自动布局约束有关。修改框架后,自动布局会将其恢复为原始大小。

我放弃了原来的方法,转而采用以下方法:

  1. 在 Interface Builder 中创建宽度 and/or 高度约束。我使用默认优先级 1000(强制约束)。
  2. 创建宽度 and/or 高度 NSLayoutConstraint 的插座。这在 IB 中很棘手:我必须在检查器的测量选项卡中双击约束,然后在检查器中打开约束对象。然后您可以 select 连接选项卡并连接插座。
  3. 在我的 NSViewController 子类中,我使用锚点来定义新的高度或宽度: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")
}