2012-06-19 4 views
5

CABasicAnimation으로 생성 된 회전 애니메이션을 사용하고 있습니다. UIView을 2 초 이상 회전합니다. 하지만 UIView을 만지면 멈출 수 있어야합니다. 애니메이션을 제거하면 뷰는 애니메이션이 시작되기 전과 같은 위치에 있습니다. 이 감동을 때,특정 지점에서 CABasicAnimation을 중지하십시오.

float duration = 2.0; 
float rotationAngle = rotationDirection * ang * speed * duration; 
//rotationAngle = 3*(2*M_PI);//(double)rotationAngle % (double)(2*M_PI) ; 
CABasicAnimation* rotationAnimation; 
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.toValue = [NSNumber numberWithFloat: rotationAngle ]; 
rotationAnimation.duration = duration; 
rotationAnimation.cumulative = YES; 
rotationAnimation.removedOnCompletion = NO; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
rotationAnimation.fillMode = kCAFillModeForwards; 
rotationAnimation.delegate = self; 

[self.view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

가 어떻게이있는 UIView의 회전 권한을 중지 할 수 있습니다

여기 내 애니메이션 코드입니까? 터치 부분을 관리하는 방법을 알고 있지만 애니메이션의 현재 각도에서 뷰를 멈추는 방법을 알 수는 없습니다.

해결책 : 프리젠 테이션 레이어의 각도를 가져 와서 애니메이션을 제거하고 뷰의 변환을 설정하여 문제를 해결했습니다. 여기 코드는 다음과 같습니다

[self.view.layer removeAllAnimations];  
CALayer* presentLayer = self.view.layer.presentationLayer; 
float currentAngle = [(NSNumber *)[presentLayer valueForKeyPath:@"transform.rotation.z"] floatValue]; 
self.view.transform = CGAffineTransformMakeRotation(currentAngle); 
+1

? 나는 항상 각도를 얻고 있습니다 0.0000 – Hisenberg

답변

15

좋은 질문! 이를 위해 Core Animation 아키텍처를 아는 것이 도움이됩니다.

Core Animation Rendering Architecture를 설명하는 Core Animation Programming Guide의 다이어그램을 보면 3 개의 트리가 있음을 알 수 있습니다.

모델 트리가 있습니다. 그것이 바로 이 원하는 값을 설정하는 곳입니다.이 발생합니다. 그러면 프리젠 테이션 트리가 있습니다. 이것은 런타임에 관한 한 꽤 많이 일어납니다. 그런 다음 마지막으로 렌더 트리입니다. 그것이 사용자가 보는 것입니다.

프리젠 테이션 트리의 값을 쿼리하려고합니다.

쉽게 할 수 있습니다. 애니메이션을 첨부 한보기의 경우 layer을 가져오고 그 값이 layer 인 경우 presentationLayer의 값을 쿼리하십시오. 예 :

CATransform3D myTransform = [(CALayer*)[self.view.layer presentationLayer] transform]; 

애니메이션 중간 흐름을 "일시 중지"할 방법이 없습니다. 값을 쿼리하고 제거한 다음 중단 한 위치에서 다시 값을 다시 작성할 수 있습니다.

약간의 고통이 있습니다.

제 다른 게시물을 좀 더 자세하게 살펴 보겠습니다.

Restoring animation where it left off when app resumes from background

당신이보기의 레이어에 애니메이션을 추가 할 때, 당신은 실제로 기본 뷰의 속성을 변경하지 않는 것 또한 잊지 마세요. 그래서 어떻게됩니까?애니메이션이 멈추고 원래의 위치에있는 모습을 볼 수있는 별난 효과를 얻게 될 것입니다.

여기에서 CAAnimation 대리자를 사용해야합니다. 나는이 포함 곳이 게시물에 대한 내 대답에서보세요 :

CABasicAnimation rotate returns to original position

위의 솔루션을 넣어 않았다
+1

프리젠 테이션 레이어에 대한 설명 주셔서 감사합니다. valueForKeyPath :로 presentationLayer의 회전 각도를 가져 와서 뷰의 변형을 설정하고 애니메이션을 제거하여 문제를 해결했습니다. –

5

당신은 presentationLayer의 회전에 회전을 설정 한 다음 레이어에서 애니메이션을 제거해야합니다. 내 블로그 게시물 Hit testing animating layers에 대한 프레 젠 테이션 레이어에 대해 읽을 수 있습니다.

뭔가 같은 것이 최종 회전을 설정하는 코드 :

self.view.layer.transform = [(CALayer*)[self.view.layer presentationLayer] transform]; 
[self.view.layer removeAnimationForKey:@"rotationAnimation"]; 
관련 문제