转场动画

#import "ViewController.h"
#define IMAGE_COUNT 4

@interface ViewController ()
{
    CATransition *_transaction;
    UIImageView *_image;
    int _currentImage;//图片名称
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _image=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    _image.contentMode=UIViewContentModeScaleAspectFill;//按比例填满屏幕
    _image.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i",_currentImage]];
    [self.view addSubview:_image];
    
    //添加手势---左划
    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:)];
    right.direction=UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:right];

}

- (void) leftSwipe : (UISwipeGestureRecognizer *) swipeGesture{
    [self transitionAnimation:YES];
}
- (void) rightSwipe : (UISwipeGestureRecognizer *) swipeGesture{
    [self transitionAnimation:NO];
}

- (void) transitionAnimation:(BOOL) isNext{
    //1.创建动画对象
    _transaction=[[CATransition alloc]init];
    //2.设置动画类型,对于苹果官方没有公开的动画类型只能使用字符串
    //cube  立方体翻转效果
    //oglFlip 翻转效果
    //suckEffect 收缩效果
    //rippleEffect 水滴波纹效果
    //pageCurl 向上翻页效果
    //pageUnCurl  向下翻页效果
    //cameralTrisHollowOpen 摄像头打开效果
    //cameraIrisHollowClose 摄像头关闭效果
    _transaction.type=@"rippleEffect";
    if (isNext) {
        _transaction.subtype=kCATransitionFromLeft;
    }else{
        _transaction.subtype=kCATransitionFromRight;
    }
    //设置动画时间
    _transaction.duration=4;
    //设置翻转后的图片
    if (isNext) {
        _currentImage=(_currentImage+1) % IMAGE_COUNT;
    }else{
        _currentImage=(_currentImage- 1 + IMAGE_COUNT) %IMAGE_COUNT;
    }
    //添加动画
    _image.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i",_currentImage]];
    [_image.layer addAnimation:_transaction forKey:@"YC"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


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