天天看點

iOS CoreAnimation 轉場動畫 CATransition

本文參考:http://www.cnblogs.com/kenshincui/p/3972100.html#autoid-3-0-0總結的:

效果:

iOS CoreAnimation 轉場動畫 CATransition
iOS CoreAnimation 轉場動畫 CATransition
iOS CoreAnimation 轉場動畫 CATransition
iOS CoreAnimation 轉場動畫 CATransition

轉場動畫就是從一個場景以動畫的形式過渡到另一個場景。轉場動畫的使用一般分為以下幾個步驟:

 1.建立轉場動畫 CATransition

 2.設定轉場類型transtion.type、子類型transtion.subtype(可選)及其他屬性

 3.設定轉場後的新視圖并添加動畫到圖層

 下表列出了常用的轉場類型(注意私有API是蘋果官方沒有公開的動畫類型,但是目前通過仍然可以使用):

 *

 公開的API

 *  fade 淡出效果 kCATransitionFade

    movein  新視圖移動到舊視圖上  kCATransitionMoveIn

    push 新視圖退出舊視圖上 kCATransitionPush

    reveal 移開舊視圖顯示新視圖  kCATransitionReveal

 私有的API

    cube 立體翻轉效果

    oglFlip  翻轉效果

    suckEffect 收縮效果

    rippleEffect 水滴波紋效果

    pageCurl 向上翻頁效果

    pageUnCurl 向下翻頁效果

    cameralIrisHollowOpen 攝像頭打開效果

    cameraIrisHollowClose  攝像頭關閉效果

//
//  TransitionViewController.m
//  CAKeyframeAnimation
//
//  Created by 帝炎魔 on 16/5/26.
//  Copyright © 2016年 帝炎魔. All rights reserved.
//

#import "TransitionViewController.h"

#define IMAGE_COUNT 10

@interface TransitionViewController (){
    
    UIImageView *_imageView;
    int _currnetIndex;
}

@end

@implementation TransitionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 定義圖檔空間
    _imageView = [[UIImageView alloc] init];
    _imageView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    _imageView.image = [UIImage imageNamed:@"fish0"];
    [self.view addSubview:_imageView];
    
    // 添加手勢
    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)];
    [self.view addGestureRecognizer:right];
    
    
    
    // Do any additional setup after loading the view.
}

#pragma mark ---- 左掃手勢
- (void)leftSwipe
{
    [self transitionAnimation:YES];
}

#pragma mark --- 右掃手勢
- (void)rightSwipe
{
    [self transitionAnimation:NO];
}

#pragma mark --- 轉場動畫
/**
 *  轉場動畫就是從一個場景以動畫的形式過渡到另一個場景。轉場動畫的使用一般分為以下幾個步驟:
 
 1.建立轉場動畫 CATransition
 
 2.設定轉場類型transtion.type、子類型transtion.subtype(可選)及其他屬性
 
 3.設定轉場後的新視圖并添加動畫到圖層
 
 下表列出了常用的轉場類型(注意私有API是蘋果官方沒有公開的動畫類型,但是目前通過仍然可以使用):
 *
 公開的API
 *  fade 淡出效果 kCATransitionFade
    movein  新視圖移動到舊視圖上  kCATransitionMoveIn
    push 新視圖退出舊視圖上 kCATransitionPush
    reveal 移開舊視圖顯示新視圖  kCATransitionReveal
 
 
 私有的API
    cube 立體翻轉效果
    oglFlip  翻轉效果
    suckEffect 收縮效果
    rippleEffect 水滴波紋效果
    pageCurl 向上翻頁效果
    pageUnCurl 向下翻頁效果
    cameralIrisHollowOpen 攝像頭打開效果
    cameraIrisHollowClose  攝像頭關閉效果
 */

- (void)transitionAnimation:(BOOL)isNext
{
    // 1. 建立轉場動畫對象
    CATransition *transtion = [[CATransition alloc] init];
    
    // 設定動畫類型, 隻能使用字元串
    transtion.type = @"cameraIrisHollowClose";
    
    // 設定子類型
    if (isNext) {
        transtion.subtype = kCATransitionFromRight;
    }else {
        transtion.subtype = kCATransitionFromLeft;
        
    }
    
    // 設定動畫時間
    transtion.duration = 1.0;
    
    // 設定轉場動畫後新的視圖添加
    _imageView.image = [self getImage:isNext];
   
    [_imageView.layer addAnimation:transtion forKey:@"KCTransitionAnimation"];
}

- (UIImage *)getImage:(BOOL)isNext
{
    if (isNext) {
        _currnetIndex = (_currnetIndex + 1)%IMAGE_COUNT;
        
    }else{
        _currnetIndex = (_currnetIndex - 1 + IMAGE_COUNT) %IMAGE_COUNT;
    }
    NSString *imageName = [NSString stringWithFormat:@"fish%i", _currnetIndex];
    return [UIImage imageNamed:imageName];
    
    

}











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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end