2013-08-07 3 views
4

UIView 자체를 회전시키지 않고 중심점을 중심으로 UIView를 회전하고 싶습니다. 관람차의 자동차와 마찬가지로 (항상 똑바로 세우십시오) 시계와는 대조적입니다.뷰 자체를 회전하지 않고 중심점을 중심으로 UIView 회전하기

내가 중심점을 중심으로 UIView를 회전 한 시간은 레이어 위치/anchorPoint/transform을 사용하여 효과를 얻었습니다. 그러나 그것은 뷰 자체의 방향을 분명히 바꿉니다.

어떤 도움이 필요합니까?

켈러

답변

8

이것은 Core Animation에서 매우 쉽게 할 수 있습니다. 이 예제에서는 꽤 지루한 정적 값을 사용 했으므로 일부 수정을 원할 것입니다. 그러나 이것은 원형 UIBezierPath를 따라 뷰를 이동하는 방법을 보여줍니다.

UIView *view = [UIView new]; 
[view setBackgroundColor:[UIColor redColor]]; 
[view setBounds:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)]; 
[view setCenter:[self pointAroundCircumferenceFromCenter:self.view.center withRadius:140.0f andAngle:0.0f]]; 
[self.view addSubview:view]; 

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(CGRectGetMidX(self.view.frame) - 140.0f, CGRectGetMidY(self.view.frame) - 140.0f, 280.0f, 280.0f)]; 

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
pathAnimation.duration = 5.0; 

pathAnimation.path = [path CGPath]; 

[view.layer addAnimation:pathAnimation forKey:@"curveAnimation"]; 

그리고 뷰의 초기 중심을 생성하는 기본 함수.

- (CGPoint)pointAroundCircumferenceFromCenter:(CGPoint)center withRadius:(CGFloat)radius andAngle:(CGFloat)theta 
{ 
    CGPoint point = CGPointZero; 
    point.x = center.x + radius * cosf(theta); 
    point.y = center.y + radius * sinf(theta); 

    return point; 
} 
-1

원의 주위를 반복해서 배치해야합니다. 현재 중심점을 계산하고 UIView animateWithDuration ... 메서드에서 해당 메서드를 계속 호출하는 메서드를 구현합니다.

관련 문제