2012-06-03 91 views
0

我的目標是我的動畫UI,和我使用UIView的動畫。問題是我需要同時進行多個動畫,但顯然我一次只能執行一個UIView動畫。我之前在這個主題上提出過問題(Multiple UIView Animations),所有響應者都說我必須用animationDidStop:performSelector:這樣的方法設置一個委託給動畫,但我想知道是否可以將子視圖添加到主視圖並同時執行動畫每個子視圖。另外,我無法執行背靠背動畫,我想也許我可以在view1上執行動畫,然後在view上執行動畫而不進行委託。的UIView子視圖的動畫

例如:

//Header 
@property (nonatomic, strong) UIView *view1; 
@property (nonatomic, retain) UIView *view2; 

//Implementation 
@synthesize view1; 
@synthesize view2; 

//ViewDidLoad 
view1 = [[UIView alloc]initWithFrame:CGRectMake(0,0,480,640)]; 
view2 = [[UIView alloc]initWithFrame:CGRectMake(0,0,480,640)]; 

view1.backgroundColor = [UIColor clearColor]; 
view2.backgroundColor = [UIColor clearColor]; 

[performAnimationsOn View1]; //I don't know the code I would put here because the code 
          //I usually use is [UIView beginAnimation:@"animation" 
          //context:nil]; but that is a class method and I am not 
          //sure how to perform an animation on a specific subview. 
[performAnimationsOn View2]; 

回答

1

我沒有看到這裏的問題,如果你需要的是動畫2點diffent意見同時做到以下幾點:

//First add these subviews 
[self.view addSubview:view1]; 
[self.view addSubview:view2]; 


[UIView beginAnimations:@"Animation1" context:nil]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:1]; 
//Do something with view 1 move, change alpha, change transformation etc..  
[UIView commitAnimations]; 

而且添加的功能

- (void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
    if ([animationID isEqualToString:@"Animation1"]) { 
     [UIView beginAnimations:@"Animation2" context:nil]; 
     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:1]; 
     //Do something with view 2 move, change alpha, change transformation etc..  
     [UIView commitAnimations]; 
    } 
    else if ([animationID isEqualToString:@"Animation2"]) { 
     [UIView beginAnimations:@"Animation3" context:nil]; 
     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:1]; 
     //Do something with view 3 move, change alpha, change transformation etc..  
     [UIView commitAnimations]; 
    } 
    //And so on..... 
} 

動畫應該按順序發生

+0

啊..確定虐待更新我的答案 –

+0

請檢查更新的答案:) –

+0

是在執行。 M檔 –

0

這聽起來像是在那裏你可以考慮使用CABasicAnimations的和潛在的一個CAAnimation組來執行你的動畫的情況。

當您使用UIView beginAnimationsContext時,您在上下文範圍內修改的隱式屬性(例如,在調用[UIView commitAnimations]之前)將同時進行動畫處理。

使用CABasicAnimations稍微複雜一點,但容納您想要的行爲類型。例如,您可以將動畫添加到特定的視圖(而不是其圖層)。

這裏有一個簡單的tutorial

+0

感謝。這也有幫助。儘管如此我仍然沒有投票。對不起 – pasawaya

+0

只有組中的所有動畫在同一圖層上操作,動畫組才能正常工作。我四處奔走,最後得到了蘋果工程師的確認。 CABasicAnimation對象功能更強大,但使用起來要複雜得多。 –

0

您可以使用多種方法,並讓animationDidStop方法依次調用每個動畫,如其他貼圖所示。

但是,使用像animateWithDuration:animations:completion這樣的基於塊的新動畫方法更簡單,更簡單。

這種方法可以讓你在得到一次在動畫完成執行完成塊傳遞。該代碼然後可以調用序列中的下一個動畫。

如果需要,動畫塊可以同時動畫多個視圖。 (所以可以beginAnimations/commitAnimations,對於這個問題,你只要更改應用到beginAnimations和commitAnimations電話之間的多個視圖。