UIView Animation의 setAnimationDidStopSelector 사용 시 주의점

2009. 10. 23. 09:54아이폰 개발

UIImageView로 만든 이미지를 애니메이션 시킬 때, UIView의 애니메이션 기능을 이용하면 정말 손쉽게 애니메이션을 구현할 수 있다. 사실, 이 부분은 별로 관심이 없었는데 지난 KGC2009 컨퍼런스에서 강연 중에 시연을 보고 아주 멋진 기능이라는 것을 알게되었다. (http://kr.blog.yahoo.com/nashorn74/1226308)

그런데, commitAnimations를 실행 시키는 순간 애니메이션이 동작되면서 바로 다음 줄의 코드도 같이 동시에 실행되기 때문에, 애니메이션이 종료된 다음에 처리해야하는 것은 setAnimationDidStopSelector를 이용하여 종료시 실행되는 셀렉터 함수를 지정하도록 되어있다. 처음에는 touchesBegan 함수 안에서 다음과 같이 코딩을 했는데, 종료시에 해당 함수를 호출하지 않는 문제가 발생하였다.

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.2];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
<UIImageView의 위치 변경>
[UIView commitAnimations];

한참을 뻘짓하다가 context를 NULL이 아닌 touchPointValue 값을 넣어주니 애니메이션 종료 후에 정상적으로 지정된 셀렉터 함수가 호출이 되었다.

UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self];
NSValue *touchPointValue = [[NSValue valueWithCGPoint:touchLocation] retain];
[UIView beginAnimations:nil context:touchPointValue];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.2];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
<UIImageView의 위치 변경>
[UIView commitAnimations];

혹시나 비슷한 문제를 겪고 있는 분들께 도움이 되길 바란다.