2012-12-27 62 views
0

我已經爲iOS遊戲寫了一個簡單的動畫,並且我試圖將它轉換爲Mac OS。可可的簡單動畫Objective-C

這裏是我的iOS

NSMutableArray *imgArr = [[[NSMutableArray alloc] init] autorelease]; 

    for(int i = 1; i < 6 + 1; i++) { 
     [imgArr addObject: [UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png", filePrefix, i]]]; 
    } 

    UIImageView * animation = [[UIImageView alloc] initWithFrame:rect]; 
    animation.animationImages = imgArr; 

    animation.animationRepeatCount = 1; 
    [animation startAnimating]; 

    [self.view addSubview:animation]; 

至於可可代碼,這部分給出了錯誤..我怎樣才能解決這個問題?

//animation.animationImages = imgArr; 

//animation.animationRepeatCount = 1; 
//[animation startAnimating]; 

與計時器

for(int i = 0; i < 6; i++) 
    [NSTimer scheduledTimerWithTimeInterval:1 
            target:self 
            selector:@selector(anim) 
            userInfo:nil 
            repeats:NO]; 


- (void)anim { 
    ++num; 


    NSImageView *animation = [[NSImageView alloc] initWithFrame:NSMakeRect(shipPosition.x, shipPosition.y, shipSize.width, shipSize.height)]; 

    [animation setImage: [NSImage imageNamed:[NSString stringWithFormat:@"explosprite%d.png", num]] ]; 
    [objectView addSubview:animation]; 

    [animation release]; 
} 
+0

我剛剛回答了[與本質相同的目標的另一個問題](http://stackoverflow.com/q/14064328/30461)。 (順便說一句,你知道你的代碼創建了一個新的NSImageView的每一幀,並沒有清理舊的,對吧?) –

回答

1

的UIImageView新代碼中不存在的可可在所有。可可實際上並不支持精靈動畫,就像你在這裏試圖做的那樣。 AFAIK最常見的方式就是設置NSTimer並在計時器觸發時適當地更新圖像。

您還可以創建處理動畫圖像的自定義CALayer子類。我不確定這一定更容易,但它是一種選擇。

+0

我知道..我認爲NSImageView也有能力做到這一點.. – user1526474

+0

@ user1526474不幸的是。 NSImageView來自一個更原始的時間。 – Chuck

+0

我曾嘗試下面的代碼,但它並不真正動畫..它只是顯示的最後的圖像..'[的NSTimer scheduledTimerWithTimeInterval:1 目標:自 選擇器:@selector(阿尼姆) USERINFO:無 重複:NO]; ' – user1526474