I'm trying to do a 360 spin of a view but at a 45 degree tilt.

Like this

enter image description here

I can't figure out for to do it.

So far I've managed this

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

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

Which spins it on it's y axis, but what I want is for this Y axis to be tilted 45 degree before it's spun.

有帮助吗?

解决方案

First, you should see the link below which explains that the differences between "frame" and "bounds". UIView frame, bounds and center

And now, here is my answer.

/* 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"];

其他提示

try this;

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"];

1- Change the anchor point to the upper right edge

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

2- rotate the view by 45 or 35 degrees

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

3- rotate or flip your view by X axis

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) {
        }
    }];

Works like a charm :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top