Cocoa - 两个 NSImage 之间的淡入淡出?

发布于 2024-10-19 16:49:58 字数 86 浏览 1 评论 0原文

我有一个带有 NSImage 的菜单,显示一些信息,当它更新时,我希望新的(更新的)图像淡入。我知道这在 iPhone 上很容易,但这在 OS X 中可能吗?

I have a menu with an NSImage showing some information, and when it gets updated I would like the new (updated) image to fade in. I know it is easy on the iPhone, but is this possible in OS X ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳哥哥 2024-10-26 16:49:58

你可以尝试使用这个。它是用 Swift 4 编写的

 let transition = CATransition() //create transition
 transition.duration = 4 //set duration time in seconds
 transition.type = .fade //animation type
 transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
 self.imageView.layer?.add(transition, forKey: nil) //add animation to your imageView's layer
 self.imageView.image = NSImage(named: "test_image") //set the image

You can try using this. It was written in Swift 4

 let transition = CATransition() //create transition
 transition.duration = 4 //set duration time in seconds
 transition.type = .fade //animation type
 transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
 self.imageView.layer?.add(transition, forKey: nil) //add animation to your imageView's layer
 self.imageView.image = NSImage(named: "test_image") //set the image
笑饮青盏花 2024-10-26 16:49:58

我认为最有价值的例子之一是 ImageTransition。至少对于来自 iOS 世界的我来说,这很容易理解。
http://developer.apple.com/library/mac/samplecode/ImageTransition /ImageTransition.zip

希望有帮助,
保罗

I think that one of the most valuable example is ImageTransition. It's easy to understand at least for me, coming from the iOS world.
http://developer.apple.com/library/mac/samplecode/ImageTransition/ImageTransition.zip

Hope it helps,
Paolo

东京女 2024-10-26 16:49:58

这取决于您的菜单如何显示图像。如果是菜单项本身,那么如果没有大量的黑客技术,这是不可能的。如果您在菜单中使用自定义 NSView,则可以使用两个 NSImageView 并使用视图的 -animator 在它们之间进行交换。

您可以将 imageViewB 的框架与 imageViewA 相匹配(如果尚未匹配),然后通过动画师替换子视图:

[[parentView animator] replaceSubview:imageViewA with:imageViewB];

...或从 b 返回到 a。

不幸的是, NSImageView 的 -animator 代理不会对 -setImage: 进行动画处理:(没有明显的原因),因此您需要使用两个视图并对交换进行动画处理。

It depends on how your menu is showing the image. If it's the menu item itself, this isn't possible without a lot of hackery. If you're using a custom NSView in the menu, then you can use two NSImageViews and swap between them using the view's -animator.

You'd match imageViewB's frame to imageViewA if it's not already, then replace the subview through the animator:

[[parentView animator] replaceSubview:imageViewA with:imageViewB];

... or back from b to a.

Unfortunately, NSImageView's -animator proxy won't animate -setImage: (for no discernible reason) so you need to use two views and animate the swap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文