如果您不喜欢内置的翻转变换,那么在iOS上使用Quartz有几种方法可以使一个视图翻转到另一个视图。以下两个代码片段都需要您导入
#import <QuartzCore/QuartzCore.h>
并链接QuartzCore框架。请注意,我编写的两个函数都假定这两个视图以相同的框架添加为子视图,并且其中一个视图被隐藏(
setHidden:
)。
第一个函数将使用2D变换将一个视图翻转到另一个视图。这更有效率,实际上它只是沿着x轴缩放。以高速运行时,它看起来很好,我认为。
+ (void)flipFromView1:(UIView*)v1 toView:(UIView*)v2 duration:(NSTimeInterval)duration completion:(void (^)(BOOL finished))completion
{
duration = duration/2;
[v2.layer setAffineTransform:CGAffineTransformMakeScale(0, 1)];
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[v1.layer setAffineTransform:CGAffineTransformMakeScale(0, 1)];
} completion:^(BOOL finished){
[v1 setHidden:YES];
[v2 setHidden:NO];
}];
[UIView animateWithDuration:duration delay:duration options:UIViewAnimationOptionCurveEaseOut animations:^{
[v2.layer setAffineTransform:CGAffineTransformMakeScale(1, 1)];
} completion:completion];
}
第二个函数执行实际的3D变换。请注意,如果使用此3D变换旋转的视图在3D空间中超过或低于其他子视图,则在此动画期间可能会看到那些其他视图突出或被隐藏。
+ (void)flipFromView2:(UIView*)v1 toView:(UIView*)v2 duration:(NSTimeInterval)duration rToL:(BOOL)rToL completion:(void (^)(BOOL finished))completion
{
duration = duration/2;
v2.layer.transform = CATransform3DMakeRotation((rToL ? -1.57079633 : 1.57079633), 0, 1, 0);
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
v1.layer.transform = CATransform3DMakeRotation((rToL ? 1.57079633 : -1.57079633), 0, 1, 0);
} completion:^(BOOL finished){
[v1 setHidden:YES];
[v2 setHidden:NO];
}];
[UIView animateWithDuration:duration delay:duration options:UIViewAnimationOptionCurveEaseOut animations:^{
v2.layer.transform = CATransform3DMakeRotation(0, 0, 1, 0);
} completion:completion];
}