iOS Core Animation (四) 转场动画

转场动画就是从一个场景以动画的形式过渡到另一个场景。一般分为以下几个步骤:
 1、创建转场动画
 2、设置转场类型、子类型(可选)及其他属性
 3、设置转场后的新视图并添加动画到图层
常用的转场类型(注意私有API是苹果官方没有公开的动画类型,但是目前通过仍然可以使用):

动画类型 说明 对应常量 是否支持方向设置
公开API
fade 淡入淡出 kCATransitionFade 是
movein 新视图移动到旧视图上 kCATransitionMoveIn 是
push 新视图推出旧视图 kCATransitionPush 是
reveal 移开旧视图显示新视图 kCATransitionReveal 是
私有API 私有API只能通过字符串访问
cube 立方体翻转效果 无 是
oglFlip 翻转效果 无 是
suckEffect 收缩效果 无 否
rippleEffect 水滴波纹效果 无 否
pageCurl 向上翻页效果 无 是
pageUnCurl 向下翻页效果 无 是
cameralIrisHollowOpen 摄像头打开效果 无 否
cameraIrisHollowClose 摄像头关闭效果 无 否

过渡方向参数:

动画类型 说明
kCATransitionFromRight 从右侧转场
kCATransitionFromLeft 从左侧转场
kCATransitionFromTop 从顶部转场
kCATransitionFromBottom 从底部转场

利用转场动画利用一个UIImageView实现一个无限循环图片浏览器。

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *shomView;
@property (weak, nonatomic) IBOutlet UIImageView *showImageView;
@property (weak, nonatomic) IBOutlet UILabel *showTitleLabel;
@property (nonatomic, assign) NSInteger index;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.shomView.layer.cornerRadius = 20.0f;
    self.shomView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.shomView.layer.borderWidth = 1;
    self.showImageView.layer.cornerRadius = 10.0f;
    self.showImageView.layer.masksToBounds = YES;
    self.shomView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.shomView.layer.shadowOffset = CGSizeMake(5, 5);
    self.shomView.layer.shadowOpacity = 0.6;
    self.index = 1;
    
    //添加手势
    UISwipeGestureRecognizer *leftSwipeGesture =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
    leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.shomView addGestureRecognizer:leftSwipeGesture];
    
    UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)];
    rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.shomView addGestureRecognizer:rightSwipeGesture];
}

#pragma mark 向左滑动
-(void)leftSwipe:(UISwipeGestureRecognizer *)gesture{
    [self transitionAnimation:YES];
}

#pragma mark 向右滑动
-(void)rightSwipe:(UISwipeGestureRecognizer *)gesture{
    [self transitionAnimation:NO];
}

#pragma mark 转场动画
-(void)transitionAnimation:(BOOL)isLove{
    self.index ++;
    // 1、创建转场动画对象
    CATransition *transition = [[CATransition alloc] init];
    // 2、设置动画类型,注意对于苹果官方没公开的动画类型只能使用字符串,并没有对应的常量定义
    transition.type = kCATransitionPush;
    // 设置子类型
    if (isLove) {
        transition.subtype = kCATransitionFromRight;
    }else{
        transition.subtype = kCATransitionFromLeft;
    }
    // 设置动画时常
    transition.duration = 1.0f;
    
    // 3、设置转场后的新视图添加转场动画
    self.showImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image_%ld.jpg", self.index%4+1]];
    self.showTitleLabel.text = [NSString stringWithFormat:@"第%ld张", self.index];
    [self.shomView.layer addAnimation:transition forKey:@"KCTransitionAnimation"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
动态图1.gif

替换transition.type参数为表1中的参数可以呈现不同的效果。

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容