2011-09-29 69 views
2

我有2個子視圖需要整個屏幕(除了狀態欄)。我們稱這個尺寸爲「屏幕尺寸」。iPhone - 同時動畫2個子視圖

欲動畫兩者:

  • 從一點點比屏幕尺寸與屏幕尺寸較大,以放大所述第一,從阿爾法0到1的α

  • 從第二屏幕尺寸略小於屏幕尺寸,從alpha 1到alpha 0.

第二個視圖是可見的,並在屏幕上開始。

我寫了這個:

- (void) switchViews 
{ 
    if (self.view2Controller == nil) { 
     self.view2Controller = [[View2Controller alloc] initWithNibName:@"View2XIB" bundle:nil]; 
     self.view2Controller.view.hidden = YES; 
     [self.view addSubview:self.view2Controller.view]; 
    } 

    CGRect bigFrame = CGRectInset(self.view.frame, -50, -50); 
    CGRect normalFrame = self.view.frame; 
    CGRect smallFrame = CGRectInset(self.view.frame, 50, 50); 

    self.view2Controller.view.frame = bigFrame; 
    self.view2Controller.view.alpha = 0.0; 

    [UIView beginAnimations:@"Anim1" context:nil]; 
    [UIView setAnimationDuration:5]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

    self.view2Controller.view.hidden = NO; 
    self.view2Controller.view.frame = normalFrame; 
    self.view2Controller.view.alpha = 1.0; 

    [UIView commitAnimations]; 

    // ------------------------------ 

    [UIView beginAnimations:@"Anim2" context:nil]; 
    [UIView setAnimationDuration:5]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self.view cache:YES]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

    self.view1Controller.view.frame = smallFrame; 
    self.view1Controller.view.alpha = 0.0; 

    [UIView commitAnimations]; 
} 

當然,我第一次嘗試把兩個動畫成一個獨特的一個。這不會改變任何事情,這就是我試圖將它們分開的原因。

啓動時,view1立即變爲黑色,然後view2按預期開始動畫。但我無法同時運行兩個動畫。

我該怎麼做?

回答

0

我看不出什麼錯在你的代碼。 所以我嘗試了你的代碼在我的項目中,它工作得很好:它確實是你想要的。

視圖1向下移動,右和淡出WHILE視圖2來自左上方和淡入...

,所以我想你必須尋找一個錯誤別的地方...

的Ciao,

盧卡

編輯:

下面的代碼:

test-switch.zip

+0

呃......你在開玩笑嗎?我會盡量隔離你今晚做的代碼。 :-) – Oliver

+0

啊啊......對不起,但是不......你的搜索字段比你發送的代碼大一點......試試我隔離的代碼(查看新的編輯答案) – meronix

2

試着看看基於塊的動畫。這是iOS 4.0+推薦的方法。看看這裏的答案:What are block-based animation methods in iPhone OS 4.0?

編輯 嘗試這樣的事情你把動畫裏面

//You can do the same thing with a frame 
CGPoint newCenter = CGPointMake(100, 100); 
[UIView animateWithDuration:2.0 
       animations:^{ 
        firstView.center = newCenter; 
        secondView.center = newCenter; 
        firstView.alpha = 0.2; 
       } 
       completion:^(BOOL finished){ 
        NSLog(@"All done animating"); 
       }]; 

什麼:^ {}將您的視圖的目標設置。上面我向你展示瞭如何改變位置以及阿爾法。

+0

我認爲這些會更適合當你想要把它們連,或讓它們依次發生(一個接一個地發生),但他希望它們發生在山姆時間。 –

+0

我得到了鏈接部分,但是他能不能只有兩個類似的塊語句彼此相鄰嗎?所以這些街區或多或少同時開火? – Cameron

+0

我現在明白你的觀點。也許,我沒有嘗試過。 –

0

我想你要使用的是一個CAAnimationGroup(核心動畫組)。

CAAnimationGroup允許將多個動畫分組並同時運行。分組動畫在CAAnimationGroup實例指定的時間空間中運行。

它有一個animations屬性,您將其設置爲您想要與setAnimations:一起執行的動畫數組。

這裏是一個usage example

+0

謝謝。你能看到我的編輯?還有一個額外的問題:在定義CAAnimationGroup時,我將不得不影響它?第一種觀點,第二種觀點?或者我想要動畫的2個子視圖的父項? – Oliver

+0

動畫組將基本包含兩個動畫的數組,每個動畫都會影響您想要動畫的不同視圖。 –