如何轻松地为NSImage制作动画?

时间:2012-05-11 14:54:45

标签: xcode animation notifications nsview

我在NSImage中创建了一个NSView,我希望通过NSNotificationCenter发送启动和停止通知来设置动画。

我必须采取哪种方式来实现这一目标?

我的代码是:

@implementation SyncToolbarItemView

- (id)init
{
    self = [super initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    if (self)
    {
        // Initialization code here.

        // Add observers
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startSyncing) name:NOTIFICATION_START_CHECK_TAG_PROCESS object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopSyncing) name:NOTIFICATION_FINISHED_CHECK_TAG_PROCESS object:nil];
    }

    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Init image
    img_sync = [[NSImageView alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, 32.0f, 32.0f)];
    [img_sync setImage:[NSImage imageNamed:@"icon_sync.png"]];

    // Add to view
    [self addSubview:img_sync];

    [img_sync release];
}

- (void) startSyncing
{

}

- (void) stopSyncing
{

}

@end

1 个答案:

答案 0 :(得分:0)

你应该看一下CoreAnimation参考,它非常清楚地说明了像NSImageView这样的控件是如何被动画化的。

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004514-CH1-SW1