如果您不喜欢内置的翻转转换,有几种方法可以在 iOS 上使用 Quartz 将一个视图翻转到另一个视图。以下两个代码片段都要求您#import <QuartzCore/QuartzCore.h>
链接到 QuartzCore 框架。请注意,我编写的两个函数都假定两个视图都作为具有相同框架的子视图添加,其中一个是隐藏的 ( setHidden:
)。
第一个函数将使用 2D 转换将一个视图翻转到另一个视图。这更有效,它真正做的只是沿 x 轴缩放。在高速下,它看起来相当不错,IMO。
+ (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];
}