簡體   English   中英

動畫的UIView子視圖

[英]UIView Subview for Animation

我的目標是為UI設置動畫,並且正在使用UIView動畫。 問題是我一次需要一個以上的動畫,但是顯然我一次只能執行一個UIView動畫。 我以前曾問過這個主題的問題( 多個UIView動畫 ),所有響應者都說我必須使用animationDidStop:performSelector:類的方法設置動畫的委托animationDidStop:performSelector:但是我想知道是否可以改為將子視圖添加到主視圖中並在每個子視圖上同時執行動畫。 另外,我無法執行背對背動畫,並且我想也許可以在不委托的情況下在view1然后在view2上執行動畫。

例如:

//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];

如果您需要立即為兩個不同的視圖制作動畫,則在這里看不到問題,請執行以下操作:

//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.....
}

動畫應順序發生

這聽起來像是您可能考慮使用CABasicAnimations以及可能使用CAAnimation組來執行動畫的情況。

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

使用CABasicAnimations稍微復雜一些,但是可以適應您想要的行為類型。 例如,您可以將動畫添加到特定視圖(而不是它們的圖層)。

這是一個簡單的教程

您可以使用多種方法,並讓animationDidStop方法依次調用每個動畫,如其他海報所建議的那樣。

但是,使用新的基於塊的動畫方法(例如animateWithDuration:animations:completion)更加簡潔方便。

該方法允許您傳遞一個完成塊,動畫完成后立即執行該完成塊。 然后,該代碼可以調用序列中的下一個動畫。

如果需要,動畫塊可以同時為多個視圖設置動畫。 (對此,beginAnimations / commitAnimations也可以。您只需將更改應用於beginAnimations和commitAnimations調用之間的多個視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM