2012-10-10 21 views
5

Tôi đang làm động một cánh tay đồng hồ từ hướng tới 12 giờ đến thời điểm hiện tại. Nếu nói, 11 giờ, tôi muốn cánh tay xoay theo chiều kim đồng hồ đến vị trí 11 giờ. Nhưng tất nhiên nếu tôi sử dụng:Xoay một chiều kim đồng hồ theo chiều kim đồng hồ cho một góc lớn hơn 180 độ

CGAffineTransform rotation = CGAffineTransformMakeRotation(2*M_PI*11/12); 

[UIView animateWithDuration:3.0 
        animations:^{ 
         clockArm.transform = rotation; 
        }]; 

vòng quay ngược chiều kim đồng hồ. Tôi đã thử:

CGFloat angle = 2*M_PI*11/12; 
CGAffineTransform firstRotation = CGAffineTransformMakeRotation(M_PI-0.001); 
CGFloat firstRotationTime = 3.0*(M_PI/angle); 
CGAffineTransform secondRotation = CGAffineTransformMakeRotation(angle); 
CGFloat secondRotationTime = 3.0 - firstRotationTime; 

[UIView animateWithDuration:firstRotationTime 
         delay:0.0 
        options:UIViewAnimationCurveLinear 
       animations:^{ 
         self.clockArm1.transform = firstRotation; 
        } 
       completion:^(BOOL finished) { 
        [UIView animateWithDuration:secondRotationTime 
              delay:0.0 
             options:UIViewAnimationCurveLinear 
             animations:^{ 
              self.clockArm1.transform = secondRotation; 
             } 
             completion:^(BOOL finished){ 
             }]; 
       }]; 

Hoạt ảnh không hoạt động theo chiều kim đồng hồ, nhưng nó vẫn bị thay đổi - hoạt ảnh dường như vẫn đang hoạt ảnh đầu tiên. Tôi đang làm gì sai, hoặc có cách nào khác để hoàn thành những gì tôi muốn?

+0

Hãy nhìn vào câu trả lời chấp nhận của http://stackoverflow.com/questions/9844925/uiview-infinite -360-degree-rotation-animation. cái đó có giúp ích không? –

Trả lời

5

Bạn có thể sử dụng các khối hoàn thành CATransaction để thiết lập thuộc tính quay của xem khi phim hoạt hình đã hoàn tất. Các chức năng sau đây làm việc trong trường hợp thử nghiệm của tôi:

- (void) rotateViewAnimated:(UIView*)view 
       withDuration:(CFTimeInterval)duration 
        byAngle:(CGFloat)angle 
{ 
    [CATransaction begin]; 
    CABasicAnimation *rotationAnimation; 
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
    rotationAnimation.byValue = [NSNumber numberWithFloat:angle]; 
    rotationAnimation.duration = duration; 
    rotationAnimation.removedOnCompletion = YES; 

    [CATransaction setCompletionBlock:^{ 
     view.transform = CGAffineTransformRotate(view.transform, angle); 
    }]; 

    [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
    [CATransaction commit]; 
} 

Bạn sử dụng nó như

[self rotateViewAnimated:self.clockArm1 withDuration:3.0 byAngle:2*M_PI*11./12.]; 
+0

Điều đó dường như hoạt động, cảm ơn! – Darren

1

Như đã đề cập trong ý kiến, hãy thử này iphone UIImageView rotation hoặc UIView Infinite 360 degree rotation animation?

#import <QuartzCore/QuartzCore.h> 

- (void) runSpinAnimationWithDuration:(CGFloat) duration; 
{ 
    CABasicAnimation* rotationAnimation; 
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ]; 
    rotationAnimation.duration = duration; 
    rotationAnimation.cumulative = YES; 
    rotationAnimation.repeatCount = 1.0; 
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

    [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 
} 
+0

Cảm ơn, đã làm việc cho các hình ảnh động, nhưng đối với một số lý do sau khi hoạt hình hoàn thành bàn tay đồng hồ của tôi trở về vị trí 12 giờ. Tôi đã thử xem tài liệu về CABasicAnimation, CAPropertyAnimation và CAAnimation. Tôi đã thử đặt thuộc tính removeOnCompletion thành NO, nhưng tôi đã không thể tìm ra cách để chuyển đổi vẫn tồn tại. – Darren

3

Cảm ơn cho câu trả lời chấp nhận @ Martin R 's. Tôi thay đổi nội dung một chút:

tôi thay thế dòng này

rotationAnimation.removedOnCompletion = YES; 

với hai dòng sau:

rotationAnimation.removedOnCompletion = NO; 
rotationAnimation.fillMode = kCAFillModeForwards; 

Sau đó, trong CompletionBlock, tôi bổ sung thêm một dòng:

[view.layer removeAllAnimations]; 

Vì vậy, mã cuối cùng sẽ trở thành:

- (void) rotateViewAnimated:(UIView*)view 
      withDuration:(CFTimeInterval)duration 
       byAngle:(CGFloat)angle 
    { 
     [CATransaction begin]; 
     CABasicAnimation *rotationAnimation; 
     rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
     rotationAnimation.byValue = [NSNumber numberWithFloat:angle]; 
     rotationAnimation.duration = duration; 
     // changed by me 
     rotationAnimation.removedOnCompletion = NO; 
     rotationAnimation.fillMode = kCAFillModeForwards; 


     [CATransaction setCompletionBlock:^{ 
     view.transform = CGAffineTransformRotate(view.transform, angle); 
     // added by me 
     [view.layer removeAllAnimations]; // this is important 

     }]; 

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

    } 

tài liệu tham khảo của tôi là từ:

1, https://stackoverflow.com/a/3586433/2481444

2, After rotating a CALayer using CABasicAnimation the layer jumps back to it's unrotated position

Các vấn đề liên quan