我想切换 NSImageView
中显示的图像,但我想为该更改设置动画。我尝试了各种方法来做到这一点。希望你们中的一位能够推荐一种可能真正有效的方法。我正在使用 Cocoa for Mac。
最佳答案
据我所知,NSImageView
不支持动画图像更改。但是,您可以将第二个 NSImageView 放置在第一个 NSImageView 之上,并以动画方式隐藏旧的并显示新的。例如:
NSImageView *newImageView = [[NSImageView alloc] initWithFrame: [imageView frame]];
[newImageView setImageFrameStyle: [imageView imageFrameStyle]];
// anything else you need to copy properties from the old image view
// ...or unarchive it from a nib
[newImageView setImage: [NSImage imageNamed: @"NSAdvanced"]];
[[imageView superview] addSubview: newImageView
positioned: NSWindowAbove relativeTo: imageView];
[newImageView release];
NSDictionary *fadeIn = [NSDictionary dictionaryWithObjectsAndKeys:
newImageView, NSViewAnimationTargetKey,
NSViewAnimationFadeInEffect, NSViewAnimationEffectKey,
nil];
NSDictionary *fadeOut = [NSDictionary dictionaryWithObjectsAndKeys:
imageView, NSViewAnimationTargetKey,
NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey,
nil];
NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations:
[NSArray arrayWithObjects: fadeOut, fadeIn, nil]];
[animation setAnimationBlockingMode: NSAnimationBlocking];
[animation setDuration: 2.0];
[animation setAnimationCurve: NSAnimationEaseInOut];
[animation startAnimation];
[imageView removeFromSuperview];
imageView = newImageView;
[animation release];
如果您的 View 很大并且可能需要 10.5+,那么您可以使用 Core Animation 做同样的事情,它将进行硬件加速并使用更少的 CPU。
创建 newImageView 后,执行以下操作:
[newImageView setAlphaValue: 0];
[newImageView setWantsLayer: YES];
// ...
[self performSelector: @selector(animateNewImageView:) withObject: newImageView afterDelay: 0];
- (void)animateNewImageView:(NSImageView *)newImageView;
{
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration: 2];
[[newImageView animator] setAlphaValue: 1];
[[imageView animator] setAlphaValue: 0];
[NSAnimationContext endGrouping];
}
您需要修改上面的内容才能中止,但我不会为您编写所有代码:-)
关于cocoa - 如何在 NSImageView 中设置内容切换动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2795882/