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
相關文章
相關標籤/搜索