我们定义了一个动画类来实现视图切换的动画效果,这个类只包含一个类方法,可直接调用,具体代码如下:
头文件:
#import@interface ViewAnimation : NSObject/*============================页面切换的方法============================== View1 表示当前页面 View2 表示目标页面 VC 两个view所在的viewController 共有12种动画效果和四个动画方向,这里采用随机挑选动画和方向的方案======================================================================*/+(void)TransView1:(UIView*)v1 View2:(UIView*)v2 VC:(UIViewController*)vc;@end
体文件:
#import "ViewAnimation.h"#import#define kDuration 0.4 // 动画效果持续时间(秒)@implementation ViewAnimation// 页面切换的方法+(void)TransView1:(UIView*)v1 View2:(UIView*)v2 VC:(UIViewController *)vc;{ CATransition *animation = [CATransition animation]; animation.delegate = self; animation.duration = kDuration; animation.timingFunction = UIViewAnimationCurveEaseInOut; switch (rand()%11) { case 1: animation.type = kCATransitionFade; break; case 2: animation.type = kCATransitionPush; break; case 3: animation.type = kCATransitionReveal; break; case 4: animation.type = kCATransitionMoveIn; break; case 5: animation.type = @"cube"; break; case 6: animation.type = @"suckEffect"; break; case 7: animation.type = @"oglFlip"; break; case 8: animation.type = @"rippleEffect"; break; case 9: animation.type = @"pageCurl"; break; case 10: animation.type = @"pageUnCurl"; break; case 11: animation.type = @"cameraIrisHollowOpen"; break; case 0: animation.type = @"cameraIrisHollowClose"; break; default: animation.type = kCATransitionMoveIn; break; } switch (rand()%3) { case 0: animation.subtype = kCATransitionFromLeft; break; case 1: animation.subtype = kCATransitionFromBottom; break; case 2: animation.subtype = kCATransitionFromRight; break; case 3: animation.subtype = kCATransitionFromTop; break; default: animation.subtype = kCATransitionFromRight; break; } [vc.view addSubview:v2]; NSInteger x1 = [[vc.view subviews]indexOfObject:v1]; NSInteger x2 = [[vc.view subviews]indexOfObject:v2]; [vc.view exchangeSubviewAtIndex:x1 withSubviewAtIndex:x2]; [[vc.view layer]addAnimation:animation forKey:@"animation"];}@end