NSViewAnimation和NSAnimation提供了视图的简单动画效果。NSViewAnimation是从NSAnimation继承下来的。这个类提供了一个简便的方式去给多个视图或窗口做动画效果。动画的效果可以改变视图的位置,大小,淡入淡出。
- (id)initWithViewAnimations:(NSArray*)viewAnimations
NSString *NSViewAnimationTargetKey; NSString *NSViewAnimationStartFrameKey; NSString *NSViewAnimationEndFrameKey; NSString *NSViewAnimationEffectKey;
第一个key值所对应的对象是必须的,可以是一个NSView或NSWindow的对象。也就是指定哪一个视图或者窗口去做动画效果。
第二个key值所对应的对象就是视图或者窗口的动画起始frame。frame包括视图或窗口位置和大小。
第三个key值所对应的对象就是视图的或窗口的结束frame。
第四个key值对象的对象就是动画的效果,是淡入效果,还是淡出效果。这个属性是可选的。
完成初始化之后,设置NSViewAnimation对象的一些属性,比如动画的持续时间等。设置动画持续时间:
- (void)setDuration:(NSTimeInterval)duration启动动画方法:
- (void)startAnimation以下是一个demo
#import "AppDelegate.h"
@implementation AppDelegate
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
//以下动画实现:firstView上移50.secondView实现消失效果
- (IBAction)startAnimations:(id)sender
{
// firstView, secondView are outlets
NSViewAnimation *theAnim;
NSRect firstViewFrame;
NSRect newViewFrame;
NSMutableDictionary* firstViewDict;
NSMutableDictionary* secondViewDict;
{
firstViewDict = [NSMutableDictionary dictionaryWithCapacity:3];
firstViewFrame = [firstView frame];
[firstViewDict setObject:firstView forKey:NSViewAnimationTargetKey];
//设置视图的起始位置
[firstViewDict setObject:[NSValue valueWithRect:firstViewFrame]
forKey:NSViewAnimationStartFrameKey];
// 改变视图1的位置,并把改变之后的位置变为视图结束位置
newViewFrame = firstViewFrame;
newViewFrame.origin.x += 0; //x不变,
newViewFrame.origin.y += 50; //y加50
[firstViewDict setObject:[NSValue valueWithRect:newViewFrame]
forKey:NSViewAnimationEndFrameKey];
}
{
secondViewDict = [NSMutableDictionary dictionaryWithCapacity:3];
//设置目标视图
[secondViewDict setObject:secondView forKey:NSViewAnimationTargetKey];
//设置视图的结束位置。
NSRect viewZeroSize = [secondView frame];
viewZeroSize.size.width = 0;
viewZeroSize.size.height = 0;
[secondViewDict setObject:[NSValue valueWithRect:viewZeroSize]
forKey:NSViewAnimationEndFrameKey];
// 设置视图淡出效果
[secondViewDict setObject:NSViewAnimationFadeOutEffect
forKey:NSViewAnimationEffectKey];
}
theAnim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray
arrayWithObjects:firstViewDict, secondViewDict, nil]];
// 设置动画的一些属性.比如持续时间0.5秒
[theAnim setDuration:0.5]; // a half seconds.
[theAnim setAnimationCurve:NSAnimationEaseIn];
// 启动动画
[theAnim startAnimation];
[theAnim release];
}
点击Button运行之后:
原文:http://blog.csdn.net/u012186949/article/details/38013115