2012-06-03 7 views
0

내 목표는 내 UI에 애니메이션을 적용하고 UIView 애니메이션을 사용하고 있습니다. 문제는 한 번에 하나 이상의 애니메이션이 필요하지만 분명히 한 번에 하나의 UIView 애니메이션 만 수행 할 수 있습니다. 이전에이 주제에 대한 질문을 던졌고 (Multiple UIView Animations) 모든 응답자는 animationDidStop:performSelector:과 같은 메서드를 사용하여 애니메이션에 대한 대리자를 설정해야한다고 말했지만 대신 기본보기에 하위보기를 추가하고 동시에 애니메이션을 수행 할 수 있는지 궁금합니다. 각 하위보기. 또한, 나는 back-to-back 애니메이션을 수행 할 수 없으며 위임하지 않고 view1 및 view2에서 애니메이션을 수행 할 수 있다고 생각했습니다. 예를 들어애니메이션 용 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

내가, 여기에 문제를 볼 그나마 한 번 다음과 같이 두 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 호출 사이의 여러보기에 변경 사항을 적용하면됩니다.