转场动画-01-day4

  1 //
  2 //  ViewController.m
  3 //  07-转场动画
  4 //
  5 //  Created by mac on 16/4/19.
  6 //  Copyright © 2016年 mac. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 @interface ViewController ()
 12 
 13 @property (strong, nonatomic) UIImageView *imageView;
 14 
 15 
 16 @property (assign, nonatomic) NSInteger index;
 17 
 18 
 19 @end
 20 
 21 @implementation ViewController
 22 
 23 - (void)viewDidLoad {
 24     [super viewDidLoad];
 25     
 26     self.index = 1;
 27     
 28     //1. 创建imageView
 29     self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100)];
 30     self.imageView.image = [UIImage imageNamed:@"1.jpg"];
 31     self.imageView.backgroundColor = [UIColor whiteColor];
 32     self.imageView.userInteractionEnabled = YES;
 33     
 34     [self.view addSubview:self.imageView];
 35     
 36     //2. 添加手势
 37     [self addRegesture];
 38 }
 39 
 40 - (void)addRegesture {
 41     
 42     //1. 左边手势
 43     UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
 44     swipe.direction = UISwipeGestureRecognizerDirectionLeft;
 45     
 46     [self.imageView addGestureRecognizer:swipe];
 47     
 48     //2. 右边手势
 49     UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
 50     swiperight.direction = UISwipeGestureRecognizerDirectionRight;
 51     
 52     [self.imageView addGestureRecognizer:swiperight];
 53 }
 54 
 55 /**
 56  *  RightSwipeGesture
 57  */
 58 - (void)swipeRight:(UIGestureRecognizer *)swipe {
 59     
 60     self.index --;
 61     if (self.index == 0) {
 62         self.index = 9;
 63     }
 64     NSString *fileName = [NSString stringWithFormat:@"%li.jpg", self.index];
 65     self.imageView.image = [UIImage imageNamed:fileName];
 66     
 67     //转场动画回来
 68     CATransition *right = [CATransition animation];
 69 //    right.type = @"pageUnCurl";
 70     right.type = @"cube";
 71     right.subtype = kCATransitionFromLeft; //过渡的方向
 72     right.duration = 1.0;
 73     [self.imageView.layer addAnimation:right forKey:nil];
 74 }
 75 
 76 /**
 77  *  LeftSwipeGesture
 78  */
 79 - (void)swipeLeft:(UIGestureRecognizer *)swipe {
 80     
 81     self.index ++;
 82     if (self.index == 10) {
 83         self.index = 1;
 84     }
 85     
 86     NSString *fileName = [NSString stringWithFormat:@"%li.jpg", self.index];
 87     self.imageView.image = [UIImage imageNamed:fileName];
 88     
 89     //添加转场动:画翻页效果
 90     CATransition *left = [CATransition animation];
 91 //    left.type = @"pageCurl";
 92     left.type = @"cube";
 93     left.subtype = kCATransitionFromRight; //过度方向
 94     left.duration = 1.0;
 95     [self.imageView.layer addAnimation:left forKey:nil];
 96     
 97     NSLog(@"%li", self.index);
 98 }
 99 
100 @end
时光见证了成长,还很无知,我想一点点幼稚转为有知!
原文地址:https://www.cnblogs.com/foreveriOS/p/5408462.html