开发者

Cocoa - Fade between two NSImage's?

I have a menu with an NSImage showing some information, and when it gets updated I would like the new (u开发者_运维知识库pdated) image to fade in. I know it is easy on the iPhone, but is this possible in OS X ?


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜