2010-08-07 28 views
5

Làm thế nào để bạn xoay UIView, UIImageView hoặc CALayer hoạt hình 360 degress mà không sử dụng OpenGL ES?Làm thế nào để bạn xoay UIView, UIImageView hoặc CALayer hoạt ảnh 360˚?

+3

Bạn nhận ra rằng 360˚ xoay sẽ cung cấp cho bạn những hình ảnh chính xác cùng, phải không? – SteamTrout

+0

Tôi thấy buồn khi tôi bắt đầu nghĩ về một giải pháp trước khi tôi nhận ra điều đó. – jtbandes

+0

@Steam Trout: chỉ khi bạn muốn nó tức thời. – JeremyP

Trả lời

3

Có lẽ bạn muốn quay phim an toàn UIImage xung quanh 360 ° đầy đủ? Theo kinh nghiệm của tôi, cách để hoàn thành một vòng quay vòng tròn đầy đủ là chuỗi nhiều động tác khác nhau:

- (IBAction)rotate:(id)sender 
{ 
    [UIView beginAnimations:@"step1" context:NULL]; { 
     [UIView setAnimationDuration:1.0]; 
     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
     [UIView setAnimationDelegate:self]; 
    [imageView.transform = CGAffineTransformMakeRotation(120 * M_PI/180); 
    } [UIView commitAnimations]; 
} 

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
    if ([animationID isEqualToString:@"step1"]) { 
     [UIView beginAnimations:@"step2" context:NULL]; { 
     [UIView setAnimationDuration:1.0]; 
     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
     [UIView setAnimationDelegate:self]; 
     imageView.transform = CGAffineTransformMakeRotation(240 * M_PI/180); 
     } [UIView commitAnimations]; 
    } 
    else if ([animationID isEqualToString:@"step2"]) { 
     [UIView beginAnimations:@"step3" context:NULL]; { 
     [UIView setAnimationDuration:1.0]; 
     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
     [UIView setAnimationDelegate:self]; 
     imageView.transform = CGAffineTransformMakeRotation(0); 
     } [UIView commitAnimations]; 
    } 
} 

Tôi đã rời khỏi đường cong easeIn/easeOut (thay vì tuyến tính) tại chỗ, do đó bạn có thể xem các hình ảnh động cá nhân.

28
#import <QuartzCore/QuartzCore.h> 

CABasicAnimation *fullRotation; 
fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
fullRotation.fromValue = @(0.0); 
fullRotation.toValue = @((360.0 * M_PI)/180.0); 
fullRotation.duration = 3.5f; 
fullRotation.repeatCount = MAXFLOAT; 

[view.layer addAnimation:fullRotation forKey:@"rotation-animation"]; 

Nếu bạn muốn thay đổi các điểm neo để nó quay xung quanh sau đó bạn có thể làm

CGRect frame = view.layer.frame; 
self.anchorPoint = CGPointMake(0.5, 0.5); // rotates around center 
self.frame = frame; 
Các vấn đề liên quan