博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于CABasicAnimation一些简单的动画
阅读量:6069 次
发布时间:2019-06-20

本文共 4217 字,大约阅读时间需要 14 分钟。

今天很匆忙的写了这篇文章,希望对大家与帮助。先展示一下动画效果!

都是一些简单的动画效果 // 缩放

/***  缩放*/- (void)scale {   CALayer *scaleLayer = [[CALayer alloc] init];   scaleLayer.backgroundColor = [UIColor purpleColor].CGColor;   scaleLayer.frame = CGRectMake(50, 70, 25, 25);   scaleLayer.shadowOffset = CGSizeMake(0, 10);   scaleLayer.shadowRadius = 3.0f;   scaleLayer.shadowOpacity = 0.5f;   scaleLayer.cornerRadius = scaleLayer.bounds.size.width * 0.5;   scaleLayer.masksToBounds = YES;   [self.view.layer addSublayer:scaleLayer];      CABasicAnimation *scaleAnimation = [[CABasicAnimation alloc] init];   scaleAnimation.keyPath = @"transform.scale"; // 设置动画的方式   scaleAnimation.fromValue = [NSNumber numberWithInteger:1]; // 起点   scaleAnimation.toValue = [NSNumber numberWithInteger:2]; // 终点   scaleAnimation.duration = 1.0f; // 设置动画的时间   scaleAnimation.repeatCount = MAXFLOAT; // 设置动画的次数   scaleAnimation.autoreverses = YES; // 返回原始状态是否动画方式      [scaleLayer addAnimation:scaleAnimation forKey:@"scale"]; // 添加动画}复制代码

// 平移

/** *  平移 */- (void)position {    CALayer *positionLayer = [[CALayer alloc] init];    positionLayer.backgroundColor = [UIColor redColor].CGColor;    positionLayer.cornerRadius = 5.0f;    positionLayer.masksToBounds = YES;    positionLayer.frame = CGRectMake(50, 140, 25, 25);    [self.view.layer addSublayer:positionLayer];        CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; // 设置动画方式沿着X轴    positionAnimation.toValue = [NSNumber numberWithInteger:250];    positionAnimation.duration = 3.0f;    positionAnimation.repeatCount = MAXFLOAT;    positionAnimation.autoreverses = YES;        [positionLayer addAnimation:positionAnimation forKey:@"position"];}复制代码

// 旋转

/** *  旋转 */- (void)rotate {    CALayer *rotateLayer = [[CALayer alloc] init];    rotateLayer.backgroundColor = [UIColor blueColor].CGColor;    rotateLayer.cornerRadius = 5.0f;    rotateLayer.masksToBounds = YES;    rotateLayer.frame = CGRectMake(50, 200, 50, 50);    [self.view.layer addSublayer:rotateLayer];        CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];    rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];    rotateAnimation.duration = 2.0f;    rotateAnimation.autoreverses = NO;    rotateAnimation.repeatCount = MAXFLOAT;        [rotateLayer addAnimation:rotateAnimation forKey:@"rotate"];}复制代码

// 动画结合

/** *  动画组 */- (void)groupAnimation {    CALayer *groupLayer = [[CALayer alloc] init];    groupLayer.backgroundColor = [UIColor grayColor].CGColor;    groupLayer.frame = CGRectMake(50, 280, 25, 25);    groupLayer.cornerRadius = 10.0f;    groupLayer.masksToBounds = YES;    [self.view.layer addSublayer:groupLayer];        // 缩放    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0f];    scaleAnimation.toValue = [NSNumber numberWithFloat:2.0];    scaleAnimation.duration = 1.0f;    scaleAnimation.repeatCount = MAXFLOAT;    scaleAnimation.autoreverses = YES;        // 平移    CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation"];    positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 400)];    positionAnimation.duration = 3.0f;    positionAnimation.autoreverses = YES;        // 旋转    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];    rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];    rotateAnimation.duration = 0.8f;    rotateAnimation.autoreverses = NO;    rotateAnimation.repeatCount = MAXFLOAT;        // 动画组    CAAnimationGroup *animationGroup = [[CAAnimationGroup alloc] init];    animationGroup.duration = 2.0f;    animationGroup.autoreverses = YES;    animationGroup.repeatCount = MAXFLOAT;    animationGroup.animations = @[scaleAnimation, positionAnimation, rotateAnimation];    [groupLayer addAnimation:animationGroup forKey:@"group"];}复制代码

//iOS还有一些其他的KeyPath来实现其他的动画

//transform.rotation.x       绕X轴旋转//transform.rotation.y       绕Y轴旋转//transform.scale.x          缩放X//transform.scale.y          缩放Y//transform.translation.x    平移X//transform.translation.y    平移Y复制代码

转载地址:http://cifgx.baihongyu.com/

你可能感兴趣的文章
结构化、半结构化和非结构化问题
查看>>
CentOS 6.9编译安装LAMP环境,并部署phpMyAdmin,使用XCache优化性能
查看>>
KeepAlive
查看>>
Sharepoint 2010升级至 2013
查看>>
Linux下安装JDK
查看>>
基于RBAC权限管理
查看>>
RHCE7.0答案之修改网络配置
查看>>
查看服务器硬盘型号,raid配置
查看>>
file permissions
查看>>
tableviewcell自适应cell高度
查看>>
马哥linux高薪中级-POSTFIX邮件服务(三)
查看>>
dns 服务
查看>>
Exchange2010、2016 发送大附件
查看>>
阿里云企业邮(免费版)
查看>>
Centos7更换Yum源
查看>>
寻找总和为n的连续子数列之算法分析
查看>>
「镁客·请讲」钢铁侠张锐:外形仿人只是次要,拥有会自主决策的“运动脑”才是真的机器人...
查看>>
CentOS 6.5 apache源码安装2.0版
查看>>
Linux下快速静态编译Qt以及Qt动态/静态版本共存
查看>>
oracle11g学习笔记
查看>>