问题
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 ?
回答1:
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.
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/5142075/cocoa-fade-between-two-nsimages