2011-03-25 9 views
1

하나의 간단한 질문 :이 오래된 패션 애니메이션의 예입니다iPhone - UIView beginAnimations 대신 animateWithDuration을 사용하면 어떤 이점이 있습니까?

:이

[UIView animateWithDuration:0.5 animations:^{ 

[base setTransform:rotate]; 
[base setCenter:newCenter]; 

}]; 

처럼 쓸 수

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 

[base setTransform:rotate]; 
[base setCenter:newCenter]; 

[UIView commitAnimations]; 

어떤 이점이 새로운를 사용하여 애니메이션을 재 작성에있다 형태?

어떤 이득이 있어야합니다. 그렇지 않으면 Apple이 새로운 기능을 수행하지 못합니다.

뭐라고하시오?

답변

7

Apple은 성능 향상을 위해 변화를 만들지 않았지만 블록은 이러한 종류의 것을 표현하는 쉬운 방법이기 때문입니다. 이전에는 애니메이션이 끝날 때 셀렉터를 사용해야했습니다.

이렇게 - animateWithDuration을 사용하는 이유는 블록이 시간을 절약하고 코드를보다 명확하게 만들고 일반적으로 매우 유용하기 때문입니다.

beginAnimation을 사용해야하는 이유 : 해당 코드를 사용할 수없는 4.0 이전의 iOS 버전을 지원하기 때문입니다. Apple은 하위 호환성을 유지해야하기 때문에 두 가지 방법을 모두 제공해야합니다.하지만 문서에서는 사용 가능한 적절한 방식으로 블록 버전의 방법을 사용하는 것이 좋습니다.

+0

감사합니다. 이제 내 코드가 4.x 용이므로 새 폼을 사용하여 다시 작성합니다. – SpaceDog

0

나는 animateWithDuration이 더 새롭고 멋지다고 생각합니다. 나는 beginAnimation 이상을 사용합니다. 더 명확한 코드입니다. beginAnimation은 4.0 미만의 iOS 버전과 호환되어야 할 때 사용합니다.

그러나 어떤 경우

는 beginAnimation 더 장점, 당신은 애니메이션 매개 변수 와 함수를 작성할 때 쉽게있다. 예 :

- (void)moveSomethingWithAnimated:(BOOL)animated { 

    // Do other task 1 

    if(animated) { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.2]; 

     someView.frame = newFrame; 
     otherView.frame = newFrame; 
    } 

    if(animated) { 
     [UIView commitAnimations]; 
    } 

    // Do other task 2 
} 

대신 :

- (void)moveSomethingWithAnimated:(BOOL)animated { 

    // Do other task 1 

    if(animated) { 
     [UIView animateWithDuration:0.2 animations:^{ 
      someView.frame = newFrame; 
      otherView.frame = newFrame; 
     }]; 
    } 
    else { 
     // duplicate code, or you have to write another function for these two line bellow 
     someView.frame = newFrame; 
     otherView.frame = newFrame; 
    } 

    // Do other task 2 
} 
관련 문제