首先新建一个类,然后引入到项目中,在ViewController中导入新建的类,实现方法跳转到新建的类里面,可以看到相应的效果:
#import "ViewController.h"
typedef enum Direction{
Right = 0,
Left,
Top,
Down,
}Direction;
@interface ViewController ()
{
NSArray *ImageList;
UIImageView *showImage;
int index;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
kCATransitionFade 交叉淡化过渡
kCATransitionMoveIn 新视图移到旧视图上面
kCATransitionPush 新视图把旧视图推出去
kCATransitionReveal 将旧视图移开,显示下面的新视图
私有api 不建议使用 苹果不提供维护 并且有可能app审核不通过
pageCurl 向上翻一页
pageUnCurl 向下翻一页
rippleEffect 滴水效果
suckEffect 收缩效果 如一块布被抽走
cube 立方体效果
oglFlip 上下翻转效果
*/
ImageList = @[@"1.jpg",@"2.jpg",@"3",@"4"];
showImage = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
showImage.userInteractionEnabled = YES;
showImage.image = [UIImage imageNamed:ImageList[0]];
[self.view addSubview:showImage];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightAction:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[showImage addGestureRecognizer:swipe];
UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftAction:)];
left.direction = UISwipeGestureRecognizerDirectionLeft;
[showImage addGestureRecognizer:left];
UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongAction:)];
[showImage addGestureRecognizer:longTap];
}
#pragma mark 长按手势实现方法
- (void)LongAction:(UILongPressGestureRecognizer *)longTap{
if (longTap.state == UIGestureRecognizerStateBegan) {
Add_ViewController *next = [[Add_ViewController alloc]init];
CATransition *transition = [CATransition animation];
transition.type = @"suckEffect";
transition.duration = 1;
transition.subtype = kCATransitionFromLeft;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
// 如果使用自定义的转场动画 必须禁用系统的动画
[self.navigationController pushViewController:next animated:NO];
}
}
#pragma mark 轻烧手势实现方法
- (void)leftAction:(UISwipeGestureRecognizer *)left{
[self ChangeImageWithDirection:Left];
}
- (void)rightAction:(UISwipeGestureRecognizer *)weipe{
[self ChangeImageWithDirection:Right];
}
#pragma mark 判断滑动的方向
- (void)ChangeImageWithDirection:(Direction)direction{
// 通过滑动的方向判断是自加还是自减
index = direction == Right ? [self selfMinus]:[self selfAdd];
// 判断左右滑动的时候不同的效果
CATransition *transition = [CATransition animation];
transition.duration = 3;
transition.type = direction == Right ?@"cube":@"rippleEffect";
[showImage.layer addAnimation:transition forKey:nil];
showImage.image = [UIImage imageNamed:ImageList[index]];
}
#pragma mark 向右滑动的时候自减
- (int)selfMinus{
index --;
return index <= 0 ? (int)ImageList.count-1:index;
}
#pragma mark 向左滑动的时候自加
- (int)selfAdd{
index ++;
// 如果超出数组元素个数的范围 就返回0
// 没有超出就是自加后的值
return index >= ImageList.count ? 0:index;
}