2014-04-17 18 views
5

Tôi đang cố gắng quay 360 chế độ xem nhưng ở độ nghiêng 45 độ.Xoay vòng Animate trên iOS ở một góc

Như thế này

enter image description here

tôi không thể tìm ra cho để làm điều đó.

Cho đến nay tôi đã quản lý này

CABasicAnimation* animation = [CABasicAnimation 
           animationWithKeyPath:@"transform.rotation.y"]; 
animation.fromValue = @(0); 
animation.toValue = @(2 * M_PI); 
animation.duration = 1; 

[self.layer addAnimation:animation forKey:@"rotation"]; 

nào quay nó trên đó là trục y, nhưng những gì tôi muốn là cho trục Y này để được nghiêng 45 độ trước khi nó quay.

Trả lời

1

Đầu tiên, bạn sẽ thấy liên kết dưới đây giải thích rằng sự khác biệt giữa "khung" và "giới hạn". UIView frame, bounds and center

Và bây giờ, đây là câu trả lời của tôi.

/* add the 4 lines below */ 
self.transform = CGAffineTransformMakeRotation(M_PI_4); 
self.layer.bounds = CGRectMake(0.0f, 0.0f, 60.0f, 200.0f); 
self.layer.position = CGPointMake(150.0f, 300.0f); 

CABasicAnimation* animation = [CABasicAnimation 
           animationWithKeyPath:@"transform.rotation.y"]; 
animation.fromValue = @(0); 
animation.toValue = @(2 * M_PI); 
animation.duration = 1; 

[self.layer addAnimation:animation forKey:@"rotation"]; 
0

1- Thay đổi điểm neo đến mép trên bên phải

self.someView.layer.anchorPoint = CGPointMake(1.0, 0.5); 

2- xoay chế độ xem bằng 45 hoặc 35 độ

CGFloat radians = (M_PI/180) * (-35); 
self.someView.transform = CGAffineTransformRotate(self.someView.transform, radians); 

3- xoay hoặc lật xem của bạn bằng cách trục X

CGFloat radians = (M_PI/180) * (180); 
[UIView animateWithDuration:1 animations:^{ 
     self.someView.layer.transform = CATransform3DRotate(self.someView.layer.transform,radians , 1, 0.0, 0.0); 

    } completion:^(BOOL finished) { 
     if(finished) { 
     } 
    }]; 

trình như một say mê :)

0

thử điều này;

CABasicAnimation* animation = [CABasicAnimation 
          animationWithKeyPath:@"transform"]; 

CATransform3D rtfm = CATransform3DMakeRotation(2 * M_PI , 1.0f, 1.0f, 0.0f); 
rtfm.m34 = -0.0015f; 

animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
animation.toValue = [NSValue valueWithCATransform3D:rtfm]; 
animation.duration = 1; 

[self.layer addAnimation:animation forKey:@"rotation"]; 
0

Dưới đây là một ví dụ Xamarin iOS tôi sử dụng để vỗ góc của một nút vuông, giống như một tai chó (dễ dàng được chuyển đến obj-C):

enter image description here

Phương pháp 1: sử dụng một hình ảnh động xoay với 1 cho cả xy trục (ví dụ trong Xamarin.iOS, nhưng dễ dàng cầm tay để obj-c):

AnimateNotify(0.10, 0, UIViewAnimationOptions.CurveEaseOut | UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.BeginFromCurrentState,() => 
{ 
    // note the final 3 params indicate "rotate around x&y axes, but not z" 
    var transf = CATransform3D.MakeRotation(-1 * (nfloat)Math.PI/4, 1, 1, 0); 
    transf.m34 = 1.0f/-500; 
    Layer.Transform = transf; 
}, null); 

Cách 2: chỉ cần thêm một vòng xoay x-axis, và y-axis luân chuyển đến một CAAnimationGroup để họ chạy cùng một lúc:

AnimateNotify(1.0, 0, UIViewAnimationOptions.CurveEaseOut | UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.BeginFromCurrentState,() => 
{ 
    nfloat angleTo = -1 * (nfloat)Math.PI/4; 
    nfloat angleFrom = 0.0f ; 
    string animKey = "rotate"; 

    // y-axis rotation 
    var anim = new CABasicAnimation(); 
    anim.KeyPath = "transform.rotation.y"; 
    anim.AutoReverses = false; 
    anim.Duration = 0.1f; 
    anim.From = new NSNumber(angleFrom); 
    anim.To = new NSNumber(angleTo); 

    // x-axis rotation 
    var animX = new CABasicAnimation(); 
    animX.KeyPath = "transform.rotation.x"; 
    animX.AutoReverses = false; 
    animX.Duration = 0.1f; 
    animX.From = new NSNumber(angleFrom); 
    animX.To = new NSNumber(angleTo); 

    // add both rotations to a group, to run simultaneously 
    var animGroup = new CAAnimationGroup(); 
    animGroup.Duration = 0.1f; 
    animGroup.AutoReverses = false; 
    animGroup.Animations = new CAAnimation[] {anim, animX}; 
    Layer.AddAnimation(animGroup, animKey); 

    // add perspective 
    var transf = CATransform3D.Identity; 
    transf.m34 = 1.0f/500; 
    Layer.Transform = transf; 

}, null); 
Các vấn đề liên quan