如何實現圖片的3D旋轉,而且是不停旋轉?

wakice發表於2016-10-13
0

我的程式碼是這樣的:

//  ViewController.m
//  核心動畫
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [UIView  animateWithDuration:3 animations:^{
self.imageView.layer.transform=CATransform3DMakeRotation(M_PI, 0, 1,1);
    }]; 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end

但是這樣做旋轉了一次就不在旋轉了。請問如何實現不停的旋轉呢?(按照Y軸)

2個回答

1
[UIView animateWithDuration:3
                      delay:0
                    options:UIViewAnimationOptionRepeat
                 animations:^{

                 }
                 completion:^(BOOL finished) {

                 }];
1

LZ的程式碼,並不是按照Y找轉,Y,Z同時都會動,按Y軸迴圈轉動,有以下兩種方法可供參考,推薦第一種方法,過渡更平滑,自然.方法一:

    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 3;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;
    
    [self.imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

方法二:

[UIView animateWithDuration:3
                      delay:0
                    options:UIViewAnimationOptionRepeat
                 animations:^{
                     self.imageView.layer.transform=CATransform3DMakeRotation(M_PI, 0, 1, 0);
                 }
                 completion:^(BOOL finished) {
                     self.imageView.layer.transform=CATransform3DMakeRotation(M_PI, 0, 1, 0);
                 }];

相關文章