iOS CoreAnimation 转场动画 CATransition

本文参考:http://www.cnblogs.com/kenshincui/p/3972100.html#autoid-3-0-0总结的:


效果:


转场动画就是从一个场景以动画的形式过渡到另一个场景。转场动画的使用一般分为以下几个步骤:
 
 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值