使用NSAnimationContext.runAnimationGroup(_:_:) (如NSAnimationContext文档中所演示的那样)对某些视图类型(包括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的框架吗?
发布于 2016-09-23 19:26:51
我怀疑这与运行时生成的隐式autolayout约束有关。修改帧后,自动收费表简单地将其恢复到原来的大小。
我放弃了原来的方法,转而采用以下方法:
NSLayoutConstraint创建一个出口。这在IB中是很棘手的:我必须双击检查器的度量衡选项卡中的约束,然后在检查器中打开约束对象。然后您可以选择connections选项卡并连接出口。NSViewController子类中,我使用锚来定义新的高度或宽度:theWidthConstraint.constant = 200和self.view.needsUpdateConstraints = true这种方法更干净,与自动收费系统更兼容。此外,它还可以轻松地动画化由新的autolayout产生的整个布局更改:
NSAnimationContext.runAnimationGroup({(let context) -> Void in
context.duration = 1.0
self.theWidthConstraint.animator().constant = 200
// Other constraint modifications can go here
}) {
print("Animation done")
}https://stackoverflow.com/questions/39618005
复制相似问题