objective-c 转场动画

ViewController.文件

//
//  ViewController.m
//  转场动画
//
//  Created by DC017 on 15/12/22.
//  Copyright © 2015年 DC017. All rights reserved.
//

#import "ViewController.h"
#define W [UIScreen mainScreen].bounds.size.width
#define H [UIScreen mainScreen].bounds.size.height
#define YANSE(r,g,b,a) [UIColor colorWithRed:r/225.0 green:g/225.0 blue:b/225.0 alpha:a]
//设置layer的宽和高
#define LayerWidth 50
#define TUPIANSHULIANG 6


@interface ViewController ()
{
    int tupianshuliang;
    UIImageView * image;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    tupianshuliang=0;
    [self layout];
    
}
-(void)layout{
    //定义一个图片控件
    image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, W, H)];
    image.contentMode=UIViewContentModeScaleToFill;
    image.image=[UIImage imageNamed:@"0"];
    [self.view addSubview:image];
    
    //添加手势
    UISwipeGestureRecognizer * leftSwipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(zuo:)];
    //设置手势方向
    leftSwipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipeGesture];
    //添加手势
    UISwipeGestureRecognizer * congzuoxiangyou=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(you:)];
    //设置手势方向
    congzuoxiangyou.direction=UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:congzuoxiangyou];
    
}
-(void)zuo:(UISwipeGestureRecognizer *)gesture{
    NSLog(@"从右往左滑");
    [self transitionAnimation:YES];//表示下一张图片
   
}
-(void)you:(UISwipeGestureRecognizer *)gesture{
     NSLog(@"从左向右滑");
    [self transitionAnimation:NO];//表示上一张图片
    
    
}
#pragma mark 转场动画
-(void)transitionAnimation:(BOOL)zhi{
    //创建动画对象
    CATransition * transactionII=[[CATransition alloc]init];
    //设置转场动画
    transactionII.type=@"cube";
    //设置动画类型,对于苹果官方没有公开的动画类型,只能使用字符串
    //cube                   立方体翻转效果
    //oglFlip                翻转效果
    //suckEffect             收缩效果
    //rippleEffect           水滴波纹效果
    //pageCurl               向上翻页效果
    //pageUnCurl             向下翻页效果
    //cameraIrisHollowOpen   摄像头打开效果
    //cameraIrisHollowClose  摄像头关闭效果
    
    
    //设置子类型(转场动画从什么方向)
    if (zhi) {
        transactionII.subtype=kCATransitionFromRight;
        
    }else{
        transactionII.subtype=kCATransitionFromLeft;
    }
    //设置动画时间
    transactionII.duration = 1.0;
    //设置转场后的新视图
    if (zhi) {
        //下一张
        tupianshuliang=(tupianshuliang+1)%TUPIANSHULIANG;
    }else{
        //上一张
        tupianshuliang=(tupianshuliang-1 +TUPIANSHULIANG)%TUPIANSHULIANG;
    }
    
    image.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i",tupianshuliang]];
    //添加动画
    [image.layer addAnimation:transactionII forKey:@"wodedonghau"];
  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


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