CATransition_图片浏览效果

#import "ViewController.h"

#define ImageCount 5

@interface ViewController ()

@property (strong, nonatomic) UIImageView *imageView;

@property (assign, nonatomic) NSInteger currentIndex;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    //保持图片原比例
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.image = [UIImage imageNamed:@"1"];
    [self.view addSubview:self.imageView];
    

    //添加左轻扫手势
    //UISwipeGestureRecognizer: 手势类型默认只能向右滑动, 触发方法
    UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
    //所以为了区别手势的滑动方向, 我们需要给向左滑动的手势指定滑动方向
    left.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:left];
    
    
    //添加右轻扫手势
    UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(RightSwipe:)];
    
    [self.view addGestureRecognizer:right];
    
}

//设置左滑动手势
- (void)leftSwipe:(UISwipeGestureRecognizer *)gesture{
    
    //结合手势方向, 指定是否是下一张(当向左滑动是, 需要展示下一张)参数为:YES
    [self transitionAnimation:YES];
    
}

//设置右滑动手势
- (void)RightSwipe:(UISwipeGestureRecognizer *)gesture{
    
    //当向右滑动时, 需要展示上一张, 所以参数为:NO
    [self transitionAnimation:NO];
    
}

//手势方法中, 判断是上一张, 还是下一张, 传递参数
- (void)transitionAnimation:(BOOL)isNext{
    
    //1. 创建转场动画
    CATransition *transition = [[CATransition alloc] init];
    
    //2. 效果
    transition.type  = @"cube";
    
    //3. 向左滑动
    if (isNext) {
        
        //设置动画的过度方向, 从右开始向左翻转
        transition.subtype = kCATransitionFromRight;
        
    }
    //向右滑动
    else{
        
        ////设置动画的过度方向, 从左开始向右翻转
        transition.subtype = kCATransitionFromLeft;
    }
    
    //4. 设置动画持续时间
    transition.duration = 1.0;
    
    //5. 调用自定义方法, 获取图片
    self.imageView.image = [self getImage:isNext];
    
    //6. 添加到layer层上
    [self.imageView.layer addAnimation:transition forKey:@"transition"];
    
    
}

- (UIImage *)getImage:(BOOL)isNext{
    
    //后一张
    if (isNext) {
        
        //例如: 当currentIndex = 1时, (1+1)%5 = 2;
        self.currentIndex = (self.currentIndex + 1) % ImageCount;
        
    }
    
    //前一张
    else{
        
        //例如: 当currentIndex = 1是
        self.currentIndex = (self.currentIndex - 1 + ImageCount) % ImageCount;
    }
    
    //往数组中添加图片
    NSString *imagName = [NSString stringWithFormat:@"%ld", self.currentIndex + 1];
    
    NSLog(@"%ld",self.currentIndex);
    return [UIImage imageNamed:imagName];
    
    
}

@end```

###转场效果type类型: 

![Type.png](http://upload-images.jianshu.io/upload_images/1803308-00b6e1804512b4ea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容