2011-01-31 38 views
1

當我試圖做一個視圖控制器中的兩個視圖之間的UI動畫時,我一直有這個奇怪的問題。奇怪的用戶界面視圖動畫效果

當它回到主菜單時,灰色條從中間到底部快速移動。它發生在第二個屏幕上的後退按鈕,但很難看到。因此,基本上,視圖內的部分正在飛來飛去,然後在動畫轉換完成時到達它們應該在的位置。

我知道動畫看起來有點奇怪的代碼,但在這裏是要回到主菜單的方法:

- (void)gotoMain_t{ 

NSLog(@"switch to main"); 

[UIView beginAnimations:nil context:NULL]; { 

    for(UIView *v in [containerView subviews]) { 
     v.frame = CGRectMake(0, 0, 1024, 768); 
     [UIView setAnimationDidStopSelector:@selector(removeT:finished:context:)]; 
    } 

    [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 

    MainMenu *mainMenu = [[MainMenu alloc] init]; 
    mainMenu.frame = CGRectMake(-1024, 0, 1024, 768); 
    [containerView insertSubview:mainMenu atIndex:1]; 


    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDuration:.4]; 
    [UIView setAnimationDelegate:self]; 

    [[self.view.subviews objectAtIndex:0] setFrame:CGRectMake(1024, 0, 1024, 768)]; 

    mainMenu.frame = CGRectMake(0, 0, 1024, 768); 

    [mainMenu release]; 

} 

[UIView commitAnimations];} 

任何想法?謝謝。

+0

的setAnimationDidStopSelector看起來奇怪,這不是清楚你需要設置一個循環裏面,因爲我覺得它只是獲取commitAnimation後調用。 – 2011-01-31 19:17:54

+0

你期望發生什麼? – 2011-01-31 19:18:24

+0

我只是希望視圖能夠滑向另一個方向。但你看到我在說什麼嗎?沒有明顯的原因,灰色條從中間到底部。 – VagueExplanation 2011-01-31 19:37:14

回答

1

在視圖動畫開始之前,將所有內容佈置在視圖動畫中非常重要。下面固定碼:

- (void)gotoMain_t{ 

NSLog(@"switch to main"); 

[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 

for(UIView *v in [containerView subviews]) { 
    v.frame = CGRectMake(0, 0, 1024, 768); 
} 

MainMenu *mainMenu = [[MainMenu alloc] init]; 
mainMenu.frame = CGRectMake(-1024, 0, 1024, 768); 
[containerView insertSubview:mainMenu atIndex:1]; 

[UIView beginAnimations:nil context:NULL]; { 

    [UIView setAnimationDidStopSelector:@selector(removeT:finished:context:)]; 

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDuration:.4]; 
    [UIView setAnimationDelegate:self]; 

    [[self.view.subviews objectAtIndex:0] setFrame:CGRectMake(1024, 0, 1024, 768)]; 

    mainMenu.frame = CGRectMake(0, 0, 1024, 768); 

    [mainMenu release]; 

} 

[UIView commitAnimations]; 

}

1

beginAnimations:之前,嘗試將該灰色條的座標設置爲結束位置。所以CoreAnimation不會嘗試對它進行動畫處理。如果可行,您可以調整其位置,以便動畫看起來不錯。它看起來不知何故灰色條的y位置變成了中間屏幕。