首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSAnimationContext不为NSButton提供动画帧大小

NSAnimationContext不为NSButton提供动画帧大小
EN

Stack Overflow用户
提问于 2016-09-21 13:29:43
回答 1查看 2.1K关注 0票数 5

使用NSAnimationContext.runAnimationGroup(_:_:) (如NSAnimationContext文档中所演示的那样)对某些视图类型(包括NSImageView)来说,框架原点和大小都可以正常工作。但是,除非我在动画NSButton之后添加显式的帧大小更改,否则它不能像预期的那样工作。

用于NSImageView的动画帧大小

下面的工作方式与预期的NSImageView一样。它被移动到原点,并调整到200x200:

代码语言:javascript
复制
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执行相同操作时,按钮将移动但不调整大小。

代码语言:javascript
复制
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")
}

但是,如果我在结尾添加了下面的代码行,那么在所有的动画代码之后,它就会像预期的那样工作!

代码语言:javascript
复制
self.button.frame = NSRect(x: 0, y: 0, width: 200, height: 200)

NSButton的最后一个工作清单是:

代码语言:javascript
复制
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的框架吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-23 19:26:51

我怀疑这与运行时生成的隐式autolayout约束有关。修改帧后,自动收费表简单地将其恢复到原来的大小。

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

  1. 在Interface中创建宽度和/或高度约束。我使用默认优先级1000 (约束是强制性的)。
  2. 为宽度和/或高度NSLayoutConstraint创建一个出口。这在IB中是很棘手的:我必须双击检查器的度量衡选项卡中的约束,然后在检查器中打开约束对象。然后您可以选择connections选项卡并连接出口。
  3. 在我的NSViewController子类中,我使用锚来定义新的高度或宽度:theWidthConstraint.constant = 200self.view.needsUpdateConstraints = true

这种方法更干净,与自动收费系统更兼容。此外,它还可以轻松地动画化由新的autolayout产生的整个布局更改:

代码语言:javascript
复制
NSAnimationContext.runAnimationGroup({(let context) -> Void in
    context.duration = 1.0
    self.theWidthConstraint.animator().constant = 200
    // Other constraint modifications can go here
}) {
    print("Animation done")
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39618005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档