iOS学习笔记-078.核心动画04——CATransition(转场动画)

本文介绍了iOS中的CATransition,它是CAAnimation的子类,用于创建视图之间的转场动画。内容包括转场动画的基本概念、不同过渡效果、代码实现以及使用UIView动画函数实现单视图转场的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

核心动画04——CATransition(转场动画)

一、简介

CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。iOS比Mac OS X的转场动画效果少一点

UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果

动画属性:

type:动画过渡类型
subtype:动画过渡方向
startProgress:动画起点(在整体动画的百分比)
endProgress:动画终点(在整体动画的百分比)

二、转场动画过渡效果

类型字符串效果说明关键字方向
fade交叉淡化过渡YES
push新视图把旧视图推出去YES
moveIn新视图移到旧视图上面YES
reveal将旧视图移开,显示下面的新视图YES
cube立方体翻滚效果
oglFlip上下左右翻转效果
suckEffect收缩效果,如一块布被抽走NO
rippleEffect水滴效果NO
pageCurl向上翻页效果
pageUnCurl向下翻页效果
cameraIrisHollowOpen相机镜头打开效果NO
cameraIrisHollowClose相机镜头关闭效果NO

三、转场基本代码

static int _i = 0;
//转场动画
-(void)transAnim:(NSString*)type{
    //转场代码与转场动画必须得在同一个方法当中.
    //转场代码
    _i = _i%3 + 1;
    self.imageV.image = [UIImage imageNamed:( [NSString stringWithFormat:@"%d",_i] )];

    //转场动画
    CATransition *anim = [CATransition animation];
    //设置转场动画类型
    anim.type = type;
    //起始位置
    anim.startProgress = 0.2;
    anim.endProgress = 0.9;

    [self.imageV.layer addAnimation:anim forKey:nil];
}

四、使用UIView动画函数实现转场动画——单视图

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

参数说明:

duration:动画的持续时间
view:需要进行转场动画的视图
options:转场动画的类型
animations:将改变视图属性的代码放在这个block中
completion:动画结束后,会自动调用这个block

options:转场动画的类型

UIViewAnimationOptionTransitionNone            = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft    = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight   = 2 << 20,
UIViewAnimationOptionTransitionCurlUp          = 3 << 20,
UIViewAnimationOptionTransitionCurlDown        = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve   = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop     = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom  = 7 << 20,

代码示例

//使用UIView动画函数实现转场动画
- (IBAction)viewTranstion:(id)sender {
    [UIView transitionWithView:self.imageV duration:0.3 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
        //转场代码
        _i = _i%3 + 1;
        self.imageV.image = [UIImage imageNamed:( [NSString stringWithFormat:@"%d",_i] )];
    } completion:^(BOOL finished) {
    }];
}

五、代码

//
//  ViewController.m
//  03_UIView72_转场动画
//
//  Created by 杞文明 on 17/6/15.
//  Copyright © 2017年 杞文明. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UIView *conV;
@property (weak, nonatomic) NSArray<NSString *> *typeArr;
@property (weak, nonatomic) UIButton *lastBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initArr];
    [self createButtons];
}

//初始化动画的type
-(void)initArr{
    _typeArr = [NSArray arrayWithObjects:@"fade",@"push",@"moveIn",@"reveal",@"cube",@"fade",@"oglFlip",@"suckEffect",@"rippleEffect",@"pageCurl",@"pageUnCurl",@"cameraIrisHollowOpen",@"cameraIrisHollowClose", nil];
}

//创建按钮
-(void)createButtons{
    //控件之间的空格
    int MARGIN_WIDTH = 10;
    int col = 3;
    int width = (_conV.bounds.size.width-MARGIN_WIDTH*(col+1)) / col;
    int height = 40;

    int j = 0;

    for (int i=0; i<_typeArr.count; i++) {
        j = i/col;
        NSInteger startX = MARGIN_WIDTH + (MARGIN_WIDTH+width)*(i%col);
        NSInteger startY = MARGIN_WIDTH + (MARGIN_WIDTH+height)*j;

        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(startX, startY, width, height)];
        //设置对应的值
        NSString * title = [NSString stringWithFormat:@"%@",_typeArr[i]];
        //给控件设置值和一些属性]
        button.backgroundColor = [UIColor grayColor];
        //文字
        [button setTitle:title forState:UIControlStateNormal];
        //字体颜色
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
        //给控件点击事件添加代理
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

        //.把控件添加到myView中
        [_conV addSubview:button];
    }
}

/**控件的点击事件*/
-(void)clickButton:(UIButton*)button{
    [self transAnim:button.titleLabel.text];
    //选中设置
    if(_lastBtn!=nil)
        [_lastBtn setSelected:NO];
    [button setSelected:YES];
    _lastBtn = button;
}


//使用UIView动画函数实现转场动画
- (IBAction)viewTranstion:(id)sender {
    [UIView transitionWithView:self.imageV duration:0.3 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
        //转场代码
        _i = _i%3 + 1;
        self.imageV.image = [UIImage imageNamed:( [NSString stringWithFormat:@"%d",_i] )];
    } completion:^(BOOL finished) {
    }];
}


static int _i = 1;
//转场动画
-(void)transAnim:(NSString*)type{
    //转场代码与转场动画必须得在同一个方法当中.
    //转场代码
    _i = _i%3 + 1;
    self.imageV.image = [UIImage imageNamed:( [NSString stringWithFormat:@"%d",_i] )];

    //转场动画
    CATransition *anim = [CATransition animation];
    //设置转场动画类型
    anim.type = type;
    //方向
    //anim.subtype = kCATransitionFromTop;
    //起始位置
    anim.startProgress = 0.2;
    anim.endProgress = 0.9;

    [self.imageV.layer addAnimation:anim forKey:nil];
}

@end

六、图示

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值