转场内容

//
//  ViewController.m
//  转场动画
//
//  Created by lanou3g on 16/6/8.
//  Copyright © 2016年 fwlong. All rights reserved.
//

#import "ViewController.h"

#define IMAGE_COUNT 5
@interface ViewController ()
{

    UIImageView * _imageView;
    int _currentindex;

}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //定义imageView
    _imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //这个图片绘制view里显示,并且比例不变
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    
    _imageView.image = [UIImage imageNamed:@"0.jpg"];
    //开启用户交互
    _imageView.userInteractionEnabled = YES;
    [self.view addSubview:_imageView];
    //添加手势
    UISwipeGestureRecognizer * leftSwipGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
    //手势方向
    leftSwipGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    //添加
    [self.view addGestureRecognizer:leftSwipGesture];
    
    //添加手势
    UISwipeGestureRecognizer * rightSwipGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)];
    //手势方向
    rightSwipGesture.direction = UISwipeGestureRecognizerDirectionRight;
    //添加
    [self.view addGestureRecognizer:rightSwipGesture];

    
}
-(void)leftSwipe:(UISwipeGestureRecognizer *)getSture
{
    [self transitionAnimation:YES];
}
//转场动画
-(void)transitionAnimation:(BOOL)isNext
{
    //创建对象
    CATransition * transition = [CATransition new];
    //设置动画类型,苹果官方并没有公开动画类型,只能用字符串
    //cube:立体旋转
    //pageCurl:翻页
    //oglFlip:中心旋转
    //    *  @"moveIn"                   新视图移到旧视图上面
    //    *  @"reveal"                   显露效果(将旧视图移开,显示下面的新视图)
    //    *  @"fade"                     交叉淡化过渡(不支持过渡方向)             (默认为此效果)
    //    *  @"pageCurl"                 向上翻一页
    //    *  @"pageUnCurl"               向下翻一页
    //    *  @"suckEffect"               收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)
    //    *  @"rippleEffect"             滴水效果,(不支持过渡方向)
    //    *  @"oglFlip"                  上下左右翻转效果
    //    *  @"rotate"                   旋转效果
    //    *  @"push"
    //    *  @"cameraIrisHollowOpen"     相机镜头打开效果(不支持过渡方向)
    //    *  @"cameraIrisHollowClose"    相机镜头关上效果(不支持过渡方向)
    //    tran.type = @"pageCurl";
    transition.type = @"cameraIrisHollowClose";
    //设置子类型
    if (isNext == YES) {
        //过度
        transition.subtype = kCATransitionFromRight;
    }else{
        transition.subtype = kCATransitionFromLeft;

    }
    //设置转场动画后的新视图添加转场动画
    _imageView.image = [self getImage:isNext];
    [_imageView.layer addAnimation:transition forKey:nil];
}
-(UIImage *)getImage:(BOOL)isNext
{
    if (isNext) {
        //下一张
        _currentindex = (_currentindex + 1)%IMAGE_COUNT;
    }else{
        //前一张
        _currentindex = (_currentindex - 1 + IMAGE_COUNT)%IMAGE_COUNT;
    }
    return [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",_currentindex]];
}
-(void)rightSwipe:(UISwipeGestureRecognizer *)getSture
{
   [self transitionAnimation:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

你可能感兴趣的:(转场内容)