2014-04-17 202 views
5

我正在尝试做360度旋转的视图,但是倾斜角度为45度。iOS以角度旋转动画

像这样

enter image description here

我想不出为做到这一点。

到目前为止,我管理这个

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

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

其中翻来覆去想在它的Y轴,但我想是这个Y轴倾斜45度它的纺前。

回答

1

首先,您应该看到下面的链接,它解释了“框架”和“边界”之间的差异。 UIView frame, bounds and center

现在,这是我的答案。

/* 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-更改锚点至右上边缘

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

2-旋转通过45或35度

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

3-旋转或通过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) { 
     } 
    }]; 

就像一个魅力:)

0

试试这个;

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

这里是一个Xamarin的iOS例如我使用皮瓣一个方形按钮的角,像狗耳(方便地移植到OBJ-C):

enter image description here

方法1:使用旋转动画与1两者xy轴(实例Xamarin.iOS,但容易携带到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); 

方法2:只需添加一个x-axis旋转和旋转y-axisCAAnimationGroup,使他们在同一时间运行:

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);