动画 NSImageView 翻译
Animate NSImageView translation
我正在尝试为 NSImageView 制作动画,使其向下滑动几个点并在这样做时淡入。
搜索了大约几个小时后,我只设法让 alpha 值进行动画处理。
翻译视图有效,但它不是动画,我不明白为什么。
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var logo: NSImageView!
@IBAction func doAnimation(_ sender: Any) {
animateLogo()
}
private func animateLogo() {
let top = CATransform3DMakeTranslation(0, 30, 0)
logo.wantsLayer = true
NSAnimationContext.runAnimationGroup({ (context) in
Swift.print("Running animation")
context.duration = 5
logo.animator().alphaValue = 0
logo.layer?.transform = top
}, completionHandler: {
Swift.print("Finished animation")
})
}
}
我尝试翻译 NSView 的另一种方法是
logo.animator().translateOrigin(to: new_origin)
如果您使用自动布局,您可以将 outlet
添加到您的约束之一,并通过 animator
.
更改 constant
值
例子
这里我有一个带有标签和按钮的视图
标签有两个约束。
- 它在视图中水平居中
- 它与视图底部的距离为 80
我现在可以在文档大纲中找到 "distance to bottom" 出口
然后按住 ctrl 键将它拖到我对应的 NSViewController
,我在其中为它创建了一个 outlet
。
@IBOutlet weak var distanceToBottom: NSLayoutConstraint!
我可以通过查看屏幕右侧的连接检查器来验证 outlet
是否已设置
最后一步是动画视图。在这里,我只是在 NSButton
上使用 IBAction
,如下所示:
@IBAction func animate(_ sender: NSButton) {
distanceToBottom.animator().constant = 5
}
如果我 运行 我的标签很好地动画到距底部 5 像素的新位置。
希望对你有所帮助。
我正在尝试为 NSImageView 制作动画,使其向下滑动几个点并在这样做时淡入。 搜索了大约几个小时后,我只设法让 alpha 值进行动画处理。 翻译视图有效,但它不是动画,我不明白为什么。
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var logo: NSImageView!
@IBAction func doAnimation(_ sender: Any) {
animateLogo()
}
private func animateLogo() {
let top = CATransform3DMakeTranslation(0, 30, 0)
logo.wantsLayer = true
NSAnimationContext.runAnimationGroup({ (context) in
Swift.print("Running animation")
context.duration = 5
logo.animator().alphaValue = 0
logo.layer?.transform = top
}, completionHandler: {
Swift.print("Finished animation")
})
}
}
我尝试翻译 NSView 的另一种方法是
logo.animator().translateOrigin(to: new_origin)
如果您使用自动布局,您可以将 outlet
添加到您的约束之一,并通过 animator
.
constant
值
例子
这里我有一个带有标签和按钮的视图
标签有两个约束。
- 它在视图中水平居中
- 它与视图底部的距离为 80
我现在可以在文档大纲中找到 "distance to bottom" 出口
然后按住 ctrl 键将它拖到我对应的 NSViewController
,我在其中为它创建了一个 outlet
。
@IBOutlet weak var distanceToBottom: NSLayoutConstraint!
我可以通过查看屏幕右侧的连接检查器来验证 outlet
是否已设置
最后一步是动画视图。在这里,我只是在 NSButton
上使用 IBAction
,如下所示:
@IBAction func animate(_ sender: NSButton) {
distanceToBottom.animator().constant = 5
}
如果我 运行 我的标签很好地动画到距底部 5 像素的新位置。
希望对你有所帮助。