下面的页面跳转实现方法是系统自带的。因为习惯使用masonry布局,所以没有用xib。
#import "ViewController.h"
#import "Masonry.h"
#define KIMG1 @"01.jpg"
#define KIMG2 @"02.jpg"
#define kDURATION 0.7f
@interface ViewController ()
@property(nonatomic,assign) int subType;
@property(nonatomic,retain) NSArray *kTitleArr;
@end
typedef enum : NSUInteger {
Fade=1, //淡入淡出
PageCurl, //翻页
Push, //推挤
PageUnCurl, //反翻页
Reveal, //揭开
CameraIrisHollowOpen, //开镜头
MoveIn, //覆盖
CameraIrisHolowClose, //关镜头
Cube, //立方体
CurlDown, //下翻页
SuckEffect, //吮吸
CurlUp, //上翻页
OglFlip, //翻转
FlipFromLeft, //左翻转
RippleEffect, //波纹
FlipFromRight, //右翻转
} AnimationType;
@implementation ViewController
#pragma mark - Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
_subType=0;
[self addBgImageWithImageName:KIMG1];
[self configButtons];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Private Methods
-(void)configButtons
{
CGFloat width=([UIScreen mainScreen].bounds.size.width-30)/2;
CGFloat height=([UIScreen mainScreen].bounds.size.height-100*2-20*7)/8;
for (int i=1; i<17; i++) {
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=i;
[btn setBackgroundColor:[UIColor cyanColor]];
[btn setTitle:self.kTitleArr[i-1] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
BOOL first=i%2==0?NO:YES;
CGFloat top=first?100+(i-1)/2*(height+20):100+(i/2-1)*(height+20);
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).offset(first?10:width+20);
make.top.equalTo(self.view.mas_top).offset(top);
make.width.mas_equalTo(width);
make.height.mas_equalTo(height);
}];
}
}
-(void)addBgImageWithImageName:(NSString *)imgStr
{
UIImage *image=[UIImage imageNamed:imgStr];
self.view.backgroundColor=[UIColor colorWithPatternImage:image];
}
-(void)btnAction:(UIButton *)sender
{
AnimationType kType=sender.tag;
NSString *subTypeStr;
switch (_subType) {
case 0:
subTypeStr=kCATransitionFromLeft;
break;
case 1:
subTypeStr=kCATransitionFromBottom;
break;
case 2:
subTypeStr=kCATransitionFromRight;
break;
case 3:
subTypeStr=kCATransitionFromTop;
break;
default:
break;
}
_subType+=1;
if (_subType>3) {
_subType=0;
}
switch (kType) {
case Fade: //淡入淡出
[self transitionWithType:kCATransitionFade andSubType:subTypeStr forView:self.view];
break;
case Push: //推挤
[self transitionWithType:kCATransitionPush andSubType:subTypeStr forView:self.view];
break;
case Reveal: //揭开
[self transitionWithType:kCATransitionReveal andSubType:subTypeStr forView:self.view];
break;
case MoveIn: //覆盖
[self transitionWithType:kCATransitionMoveIn andSubType:subTypeStr forView:self.view];
break;
case Cube: //立方体
[self transitionWithType:@"cube" andSubType:subTypeStr forView:self.view];
break;
case SuckEffect: //吮吸
[self transitionWithType:@"suckEffect" andSubType:subTypeStr forView:self.view];
break;
case OglFlip: //翻转
[self transitionWithType:@"oglFlip" andSubType:subTypeStr forView:self.view];
break;
case RippleEffect: //波纹
[self transitionWithType:@"rippleEffect" andSubType:subTypeStr forView:self.view];
break;
case PageCurl: //翻页
[self transitionWithType:@"pageCurl" andSubType:subTypeStr forView:self.view];
break;
case PageUnCurl: //反翻页
[self transitionWithType:@"pageUnCurl" andSubType:subTypeStr forView:self.view];
break;
case CameraIrisHollowOpen: //开镜头
[self transitionWithType:@"cameraIrisHollowOpen" andSubType:subTypeStr forView:self.view];
break;
case CameraIrisHolowClose: //关镜头
[self transitionWithType:@"cameraIrisHollowClose" andSubType:subTypeStr forView:self.view];
break;
case CurlDown: //下翻页
[self animationWithView:self.view andAnimationTransitation:UIViewAnimationTransitionCurlDown];
break;
case CurlUp: //上翻页
[self animationWithView:self.view andAnimationTransitation:UIViewAnimationTransitionCurlUp];
break;
case FlipFromLeft: //左反转
[self animationWithView:self.view andAnimationTransitation:UIViewAnimationTransitionFlipFromLeft];
break;
case FlipFromRight: //右反转
[self animationWithView:self.view andAnimationTransitation:UIViewAnimationTransitionFlipFromRight];
break;
default:
break;
}
static int i=0;
if (i==0) {
[self addBgImageWithImageName:KIMG1];
i=1;
}else{
[self addBgImageWithImageName:KIMG2];
i=0;
}
}
#pragma mark - CATransition动画实现
-(void)transitionWithType:(NSString *)type andSubType:(NSString *)subType forView:(UIView *)objView
{
//创建CATransition对象
CATransition *transition=[CATransition animation];
//设置运动时间
transition.duration=kDURATION;
//设置运动type
transition.type=type;
if (subType!=nil) {
//设置子类
transition.subtype=subType;
}
//设置运动速度
transition.timingFunction=UIViewAnimationOptionCurveEaseInOut;
[objView.layer addAnimation:transition forKey:@"animation"];
}
#pragma mark - UIView动画实现
-(void)animationWithView:(UIView *)objView andAnimationTransitation:(UIViewAnimationTransition )transition
{
[UIView animateWithDuration:kDURATION animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:transition forView:objView cache:YES];
}];
}
#pragma mark - Setter And Getter
-(NSArray *)kTitleArr
{
if (_kTitleArr==nil) {
_kTitleArr=@[@"淡化效果",@"翻页效果",@"Push效果",@"反翻页效果",@"揭开效果",@"开镜头效果",@"覆盖效果",@"关镜头效果",@"3D立方体效果",@"下翻页效果",@"吮吸效果",@"上翻页效果",@"翻转效果",@"左翻转效果",@"波纹效果",@"右翻转效果"];
}
return _kTitleArr;
}
@end