2

我有两个视图,我想使用核心动画在两个视图之间切换,为每个视图的图层设置动画。我想要的动画就像提供的一样,UIViewAnimationOptionTransitionFlipFromLeft但我无法做到。我可以让图层旋转 180 度,然后在动画停止时转换到下一个视图。如何才能做到这一点?

我使用的代码如下:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
self.view.layer.zPosition = 100;
CATransform3D transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
[animation setToValue:[NSValue valueWithCATransform3D:transform]];
[animation setDuration:.5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[animation setFillMode:kCAFillModeForwards];
[animation setRemovedOnCompletion:YES];
[animation setDelegate:self];

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

在委托中,我转换到下一个视图。但是,这没有多大意义,并且动画不像默认动画提供的那样生动。如何做到这一点?

4

2 回答 2

7

How to make it look like a 3D rotation

Your rotation doesn't look like it happens in 3D space because there is no perspective to your transform. Change the m34 value of the transform into something small like 1.0/800.0 and you should see it rotate with perspective.

CATransform3D transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
transform.m34 = 1.0/800.0;
[animation setToValue:[NSValue valueWithCATransform3D:transform]];

That value (third column, forth row) of the 3D transform controls the perspective of the transform. You can read more about 3D projection on Wikipedia if you want to know more about how it works.

How to flip two views as if they were to sides of a card

To have it seem like the two views are two sides of the same view you should add them rotate the back side π degrees and change the layer's doubleSided property to NO for both layers. Now they won't be visible when facing away from you. As you apply the rotation to both the front and back layer they will change which layer is visible. Note: rotate the front to π degrees and the back to 2π so that it completes it rotation and faces you.

于 2012-08-10T19:01:13.170 回答
5

你可以使用

    [UIView transitionFromView:aView
                    toView:bView
                  duration:0.3
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                completion:^(BOOL finished) {
                }
 ];

我发现您希望在同一个超级视图中拥有两个视图,否则事情会变得有点混乱。

于 2012-08-11T13:46:29.713 回答