[英]How can I animate a content switch in an NSImageView?
我想切換NSImageView
顯示的圖像,但我想為該更改設置動畫。 我嘗試過各種方法來做到這一點。 希望你們其中一個人可以建議一個可能真正起作用的人。 我正在使用Cocoa for Mac。
據我所知, NSImageView
不支持動畫圖像更改。 但是,您可以在第一個NSImageView
的頂部放置第二個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];
如果您的視圖很大並且您可以要求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];
}
你需要修改上面的內容才能生成,但我不打算為你編寫所有代碼:-)
您可以實現自己的自定義視圖,該視圖使用Core Animation CALayer
來存儲圖像。 設置圖層的contents
屬性時,圖像將自動從舊圖像平滑地動畫到新圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.