我想切换NSImageView
中显示的图片,但我想为该更改设置动画。我尝试过各种方法来做到这一点。希望你们其中一个人可以建议一个可能真正起作用的人。我正在使用Cocoa for Mac。
答案 0 :(得分:7)
据我所知,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];
并隐藏旧的[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];
}
并显示新的{{1}}。例如:
{{1}}
如果您的视图很大并且您可以要求10.5+,那么您可以使用Core Animation执行相同的操作,这将是硬件加速并且使用更少的CPU。
创建newImageView后,执行以下操作:
{{1}}
您需要修改上述内容才能中止,但我不打算为您编写所有代码: - )
答案 1 :(得分:5)
您可以实现自己的自定义视图,该视图使用核心动画CALayer
来存储图像。当您设置图层的contents
属性时,图像将自动从旧图像平滑地动画显示为新图像。