2010-11-13 13 views
3

我使用的UIView的動畫功能來游泳的魚的一個圖像在屏幕上,當已經存在在屏幕中間的魚,遊其關閉兩個影像的意見交換並游上一個新的。魚的選擇是從一個單獨的tableview控制器發送的。製作一個平滑的動態入口,出口,以及

要做到這一點,我米保持的UIImageView的兩個實例變量和self.fishViewNext交換self.fishViewCurrent一旦動畫完成(見下面的代碼)。

當用戶在新的表格單元格按下快,接下來的魚有機會到達之前,我的問題是。我的快速解決方案是延遲添加魚的新消息,目前無所作爲。這適用於2個快速觸摸,但不是更多。我正在尋找想法,如果有人知道我可以如何改進下面的代碼,以便動畫順利,您可以點擊任何內容,而最終顯示的圖像是最後一次點擊的圖像。這裏再次的重要方法:

創建第一圖像視圖:

- (void) createFish { 

self.fishViewCurrent = [[[UIImageView alloc] init] autorelease]; 
self.fishViewCurrent.contentMode = UIViewContentModeScaleAspectFit; 
self.fishViewCurrent.bounds = CGRectMake(0.0f, 0.0f, kFishWidth, kFishHeight); 
self.fishViewCurrent.center = CGPointMake(-kFishWidth, kFishYPos); // off the screen to the left 
[self addSubview:self.fishViewCurrent]; 

} 

當選擇的tableview細胞稱爲公共方法:

- (void) addFish:(UIImage *)fish { 

if (self.fishViewNext) { 
    [self performSelector:@selector(addFish:) withObject:(UIImage *)fish afterDelay:1.0]; 
    return; 
} 
self.fishViewNext = [[[UIImageView alloc] initWithImage:fish] autorelease]; 
self.fishViewNext.contentMode = UIViewContentModeScaleAspectFit; 
self.fishViewNext.bounds = CGRectMake(0.0f, 0.0f, kFishWidth, kFishHeight); 
self.fishViewNext.center = CGPointMake(self.bounds.size.width + kFishWidth, kFishYPos); // off the screen right 
[self addSubview:self.fishViewNext]; 

[UIView beginAnimations:@"swimFish" context:nil]; 
[UIView setAnimationDuration:2.0f]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(fishSwamOffHandler:finished:context:)]; 
[UIView setAnimationRepeatAutoreverses:NO]; 

self.fishViewNext.center = CGPointMake(self.center.x, kFishYPos); // center 
self.fishViewCurrent.center = CGPointMake(-kFishWidth, kFishYPos); // exit stage left 

[UIView commitAnimations]; 
} 

處理程序來切換「下一個」魚(的UIImageView )到'當前':

- (void)fishSwamOffHandler:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 

[self.fishViewCurrent removeFromSuperview]; 
self.fishViewCurrent = self.fishViewNext; 
self.fishViewNext = nil; 
} 

回答

1

最簡單的事情就是創建一個ivar BOOL,比如說_fishIsSwimming,然後_queuedFishView改變addFish的開頭:看起來像這樣:

- (void) addFish:(UIImage *)fish 
{ 
    // queue the most recent fish, if one was passed in 
    if(fish)  
     self.queuedFishView = [[[UIImageView alloc] initWithImage:fish] autorelease]; 

    // If something is swimming, leave now 
    if(_fishIsSwimming) 
     return; 

    // If we get here and there's no queued fish, leave now 
    if(!self.queuedFishView) 
     return; 

    // We have a queued fish and nothing is swimming, so do the queued fish next 
    // and clear the queued fish 
    _fishIsSwimming = YES; 
    self.nextFishView = self.queuedFishView; 
    self.queuedFishView = nil; 
    // Then carry on as normal 
} 

然後在你的動畫完成回調,你這樣做:

-(void) fishSwamOffHandler:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context 
{ 
    // We're done animating, so ready to do another swimming fish 
    _fishIsSwimming = NO; 

    [self.fishViewCurrent removeFromSuperview]; 
    self.fishViewCurrent = self.fishViewNext; 
    self.fishViewNext = nil; 

    // now call to add another fish with nil, so that it will use 
    // the queued fish if there is one and otherwise just early out 
    [self addFish:nil];  
}  

那麼會發生什麼事是你」會始終保持最近在魚一旦動畫結束後,您可以回落到addFish傳入的queuedFishView,然後,如果有一個魚等着去,那麼你把它。如果沒有,你只是等待通過按下一個按鈕

+0

是該解決方案是保持高德多了各種意見發送下一個。謝謝你的詳細解答,對不起,我花了這麼長時間才做檢查。 :) – 2011-06-28 08:22:15