IOS 转场动画 -CATransition

//

//  ViewController.m

//  转场动画-CATransition

//

//  Created by dc008 on 15/12/22.

//  Copyright © 2015 崔晓宇. All rights reserved.

//


#import "ViewController.h"

#define IMAGE_COUNT 10//图片数量

@interface ViewController ()

{

    UIImageView *_imageView;

    int _current;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    _imageView = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    _imageView.contentMode = UIViewContentModeScaleAspectFill;

    _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",_current]];

    [self.view addSubview:_imageView];

    

    //添加手势

    UISwipeGestureRecognizer *leftSwipeGesure = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];

    leftSwipeGesure.direction = UISwipeGestureRecognizerDirectionLeft;//设置手势方向

    [self.view addGestureRecognizer:leftSwipeGesure];

    

    UISwipeGestureRecognizer *rightSwipeGesure = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];

    rightSwipeGesure.direction = UISwipeGestureRecognizerDirectionRight;

    [self.view addGestureRecognizer:rightSwipeGesure];

}


- (void)rightSwipe : (UISwipeGestureRecognizer *)gesture{

    NSLog(@"向右滑动");

    [self transitionAnimation:YES];//YES表示下一张图片

}


- (void)leftSwipe : (UISwipeGestureRecognizer *)gesture{

    NSLog(@"向左滑动");

    [self transitionAnimation:NO];//NO表示上一张图片

}


#pragma mark 转场动画

- (void)transitionAnimation : (BOOL) isNext {

    //1.创建动画对象

    CATransition *transition = [[CATransition alloc]init];

    //2.设置动画类型,对于苹果官方没有公开的动画类型只能使用字符串

    //cube                    立方体翻转效果

    //oglFlip                 翻转效果

    //suckEffect              收缩效果

    //rippleEffect            水滴波纹效果

    //pageCurl                向上翻页效果

    //pageUnCurl              向下翻页效果

    //cameralIrisHollowOpen   摄像头打开效果

    //cameralIrisHollowClose  摄像头关闭效果

    

   transition.type = @"cube";

   //设置子类型(转场动画从什么方向)

    if (isNext) {

        transition.subtype = kCATransitionFromRight;

    }

    else{

        transition.subtype = kCATransitionFromLeft;

    }

    //设置动画时间

    transition.duration = 1.0;

    

    //设置转场后的新视图

    if (isNext) {

        //下一张图片

        _current = (_current + 1) % IMAGE_COUNT;

    }

    else{

        //上一张图片

        _current = (_current - 1 + IMAGE_COUNT) % IMAGE_COUNT;

    }

    _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",_current]];

    //添加动画

    [_imageView.layer addAnimation:transition forKey:@"CXY"];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


你可能感兴趣的:(IOS 转场动画 -CATransition)