1 /*
2 CATransaction 转场动画 可以切换视图 视图控制器
3 Type 转场动画 的动画效果
4 subtype 转场动画效果方向
5
6
7 */
8 #import "ViewController.h"
9 #import "NextViewController.h"
10
11 typedef enum Direction{
12 Right = 0,
13 Left,
14 } Direction;
15
16 @interface ViewController ()
17 {
18 NSArray *imageList;
19 UIImageView *showImage;
20 int index;
21 }
22 @end
23
24 @implementation ViewController
25
26 - (void)viewDidLoad {
27 [super viewDidLoad];
28 self.view.backgroundColor = [UIColor redColor];
29
30 imageList = @[@"1",@"8fac777" ];
31 showImage = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
32 showImage.image = [UIImage imageNamed:@"1"];
33 [self.view addSubview:showImage];
34 showImage.userInteractionEnabled = YES;
35 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(RightswipeAction)];
36 swipe.direction = UISwipeGestureRecognizerDirectionRight;
37
38 [self.view addGestureRecognizer:swipe];
39
40 UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipeAction)];
41 swipe.direction = UISwipeGestureRecognizerDirectionLeft;
42
43 [self.view addGestureRecognizer:leftSwipe];
44
45 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
46 [self.view addGestureRecognizer:longPress];
47 }
48
49
50 #pragma mark--------跳转到下一个页面-----
51 - (void)longPressAction:(UILongPressGestureRecognizer *)sender{
52 if (sender.state == UIGestureRecognizerStateBegan) {
53 NextViewController *next = [[NextViewController alloc]init];
54 CATransition *animation = [CATransition animation];
55 animation.type = @"rippleEffect";
56 animation.subtype = kCATransitionFromRight;
57 animation.duration = 1;
58
59 [self.navigationController pushViewController:next animated:NO];
60 }
61
62
63 }
64
65 - (void)didCHangeImageWithDirection:(Duration)direction{
66
67 index = direction == Right ? [self countRelease] : [self countAdd];
68
69
70 CATransition *transition = [CATransition animation];
71 transition.type = direction == Right ? @"cube" : @"rippleEffect";
72 transition.subtype = direction == Right ? kCATransitionFromRight : kCATransitionFromLeft;
73 transition.duration = 1;
74 [showImage.layer addAnimation:transition forKey:nil];
75
76 showImage.image = [UIImage imageNamed:imageList[index]];
77 }
78
79 #pragma mark---------向左滑动图片加---------
80 //需要 通过方向 判断 是自加 还是自减 把计算号的值 赋给 全局变量
81 - (int)countAdd{
82 index ++;
83
84 // 如果超出了 图片数组的元素 修复成0 没有超出 返回增加后的值
85 return index = index >= imageList.count ? 0 : index;
86
87 }
88 #pragma mark---------向右滑动图片减---------
89 - (int)countRelease{
90 index --;
91 return index<0 ? (int)imageList.count-1:index;
92 }
93
94 - (void)RightswipeAction{
95 [self didCHangeImageWithDirection:Right];
96
97 }
98
99 - (void)leftSwipeAction{
100 [self didCHangeImageWithDirection:Left];
101
102 }