iOS 动画篇 (二) CAShapeLayer与CoreAnimation结合使用
接上一篇博客 iOS 动画篇(一) Core Animation
CAShapeLayer是CALayer的一个子类,使用这个类能够很轻易实现曲线的动画。
先来一个折线动画效果:
示例代码:
//1.生成path
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )]; self.shapeLayer.path = path.CGPath; //设置animation
CABasicAnimation *strokeAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
strokeAnimation.fromValue = @;
strokeAnimation.toValue = @;
strokeAnimation.duration = .f; CABasicAnimation *lineWidthAnimation = [CABasicAnimation animationWithKeyPath:@"lineWidth"];
lineWidthAnimation.fromValue = @;
lineWidthAnimation.toValue = @;
lineWidthAnimation.duration = .f; CABasicAnimation *strokeColorAnimation = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
strokeColorAnimation.fromValue = (id)([UIColor redColor].CGColor);
strokeColorAnimation.toValue = (id)([UIColor magentaColor].CGColor);
strokeColorAnimation.duration = .f; CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[strokeAnimation, lineWidthAnimation, strokeColorAnimation];
group.duration = .f;
group.fillMode = kCAFillModeForwards;
group.removedOnCompletion = NO;
[self.shapeLayer addAnimation:group forKey:@"groupAnimation"];
现在介绍CAShapeLayer,CAShapeLayer几乎所有的属性都可以用来做动画,比如说path、strokeEnd、strokeStart、lineWidth等等,利用这些属性可以实现多种曲线动画。
接下来,介绍一个CAShapeLayer与贝塞尔曲线结合的曲线动画,效果图:
代码:
//二次贝塞尔曲线
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(, self.shapeLayer.bounds.size.height / )];
[path addCurveToPoint:CGPointMake(self.shapeLayer.bounds.size.width, ) controlPoint1:CGPointMake(, ) controlPoint2:CGPointMake(, )];
self.shapeLayer.path = path.CGPath; //绘制动画
CABasicAnimation *strokeEndAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
strokeEndAnimation.fromValue = @0.5;
strokeEndAnimation.toValue = @;
strokeEndAnimation.duration = .f; [self.shapeLayer addAnimation:strokeEndAnimation forKey:@"strokeAnimation"]; CABasicAnimation *strokeStartAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
strokeStartAnimation.fromValue = @0.5;
strokeStartAnimation.toValue = @;
strokeStartAnimation.duration = .f; [self.shapeLayer addAnimation:strokeStartAnimation forKey:@"strokeStartAnimation"];
再来一个看着酷一点的loading动画,效果:
代码如下:
self.shapeLayer.backgroundColor = [UIColor clearColor].CGColor;
self.shapeLayer.strokeColor = [UIColor redColor].CGColor;
self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
self.shapeLayer.lineWidth = .f;
UIBezierPath *storkePath = [UIBezierPath bezierPathWithOvalInRect:self.shapeLayer.bounds];
self.shapeLayer.path = storkePath.CGPath;
self.shapeLayer.strokeStart = ;
self.shapeLayer.strokeEnd = 0.1; //旋转动画
CABasicAnimation *rotateAnimaiton = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotateAnimaiton.duration = .f;
rotateAnimaiton.repeatCount = CGFLOAT_MAX;
rotateAnimaiton.removedOnCompletion = NO;
rotateAnimaiton.fillMode = kCAFillModeForwards;
rotateAnimaiton.toValue = @(M_PI * ); //stroke动画
CABasicAnimation *storkeAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
storkeAnimation.duration = .f;
storkeAnimation.repeatCount = CGFLOAT_MAX;
storkeAnimation.fillMode = kCAFillModeForwards;
storkeAnimation.removedOnCompletion = NO;
storkeAnimation.toValue = @(); CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = .f;
animationGroup.repeatCount =CGFLOAT_MAX;
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.removedOnCompletion = NO;
animationGroup.animations = @[rotateAnimaiton, storkeAnimation];
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]; [self.shapeLayer addAnimation:animationGroup forKey:@"indicatorAnimation"];
现在我们来看一个CAShapeLayer与mask结合的动画
代码:
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
self.shapeLayer.mask = shapeLayer; UIBezierPath *fromPath = [UIBezierPath bezierPathWithRect:CGRectMake(, , , )];
UIBezierPath *toPath = [UIBezierPath bezierPathWithRect:CGRectMake(, , , )];
shapeLayer.path = fromPath.CGPath; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.fromValue = (id)fromPath.CGPath;
animation.toValue = (id)toPath.CGPath;
animation.duration = .f;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards; [shapeLayer addAnimation:animation forKey:@"animation"];
最后再介绍一个登录动画:
分析:这个登录动画一共分为三步
1. 在button上添加一个shapeLayer,用path属性实现layer的展开动画
2. 在展开动画结束后,为button设置一个shapeLayer的mask,利用layer的path和opacity属性实现收起按钮动画
3. 添加一个loading动画到view上
详情见代码:
- (void)viewDidLoad {
[super viewDidLoad]; NSLog(@"一个复杂一点的登录动画");
[self.shapeLayer removeFromSuperlayer]; UIButton *startButton = ({
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.backgroundColor = [UIColor purpleColor];
[btn setTitle:@"start" forState:UIControlStateNormal];
btn.frame = (CGRect){{, }, {, }};
btn.center = self.view.center; [btn addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
btn;
}); [self.view addSubview:startButton]; self.startButton = startButton; } - (IBAction)startAction:(UIButton *)sender {
[self addMaskAnimation];
} - (void)addMaskAnimation
{
CAShapeLayer *shapeLayer = [CAShapeLayer new];
shapeLayer.frame = self.startButton.bounds;
shapeLayer.fillColor = [UIColor whiteColor].CGColor;
shapeLayer.strokeColor = [UIColor whiteColor].CGColor;
shapeLayer.opacity = .3f;
shapeLayer.path = [UIBezierPath bezierPathWithRect:CGRectMake(self.startButton.bounds.size.width / , , , self.startButton.bounds.size.height)].CGPath;//不初始化则无动画效果 [self.startButton.layer addSublayer:shapeLayer]; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = 0.5f;
animation.toValue = (__bridge id)[UIBezierPath bezierPathWithRect:self.startButton.bounds].CGPath;
animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO; [shapeLayer addAnimation:animation forKey:@"shapeAnimation"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self addPackupAnimation];
});
} - (void)addPackupAnimation
{
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.startButton.bounds;
self.startButton.layer.mask = maskLayer; //path动画
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
pathAnimation.duration = 0.3f;
pathAnimation.removedOnCompletion = NO;
pathAnimation.toValue = (__bridge id)[UIBezierPath bezierPathWithArcCenter:CGPointMake(self.startButton.bounds.size.width / , self.startButton.bounds.size.height / ) radius: startAngle: endAngle:M_PI * clockwise:YES].CGPath;
pathAnimation.fromValue = (__bridge id)[UIBezierPath bezierPathWithArcCenter:CGPointMake(self.startButton.bounds.size.width / , self.startButton.bounds.size.height / ) radius:self.startButton.bounds.size.width / startAngle: endAngle:M_PI * clockwise:YES].CGPath;
pathAnimation.fillMode = kCAFillModeForwards; //透明度动画
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.duration = 0.3f;
opacityAnimation.toValue = @();
opacityAnimation.fromValue = @();
opacityAnimation.removedOnCompletion = YES;
opacityAnimation.fillMode = kCAFillModeForwards; CAAnimationGroup *group = [CAAnimationGroup new];
group.animations = @[pathAnimation];
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
group.duration = pathAnimation.duration; [maskLayer addAnimation:group forKey:@"packupAnimation"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.startButton.hidden = YES;
[self addLoadingAnimation]; });
} - (void)addLoadingAnimation
{
CAShapeLayer *shapeLayer = ({
CAShapeLayer *layer = [CAShapeLayer layer];
layer.position = self.view.center;
layer.bounds = CGRectMake(, , , );
layer.backgroundColor = [UIColor clearColor].CGColor;
layer.strokeColor = [UIColor redColor].CGColor;
layer.fillColor = [UIColor clearColor].CGColor;
layer.lineWidth = .f;
UIBezierPath *storkePath = [UIBezierPath bezierPathWithOvalInRect:layer.bounds];
layer.path = storkePath.CGPath;
layer.strokeStart = ;
layer.strokeEnd = 0.1; layer;
}); [self.view.layer addSublayer:shapeLayer]; //旋转动画
CABasicAnimation *rotateAnimaiton = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotateAnimaiton.duration = .f;
rotateAnimaiton.repeatCount = CGFLOAT_MAX;
rotateAnimaiton.removedOnCompletion = NO;
rotateAnimaiton.fillMode = kCAFillModeForwards;
rotateAnimaiton.toValue = @(M_PI * ); //stroke动画
CABasicAnimation *storkeAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
storkeAnimation.duration = .f;
storkeAnimation.repeatCount = CGFLOAT_MAX;
storkeAnimation.fillMode = kCAFillModeForwards;
storkeAnimation.removedOnCompletion = NO;
storkeAnimation.toValue = @(); CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = .f;
animationGroup.repeatCount =CGFLOAT_MAX;
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.removedOnCompletion = NO;
animationGroup.animations = @[rotateAnimaiton, storkeAnimation];
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]; [shapeLayer addAnimation:animationGroup forKey:@"indicatorAnimation"];
}
核心动画就介绍到这,你可以在这里查看demo。
个人原创,转载请注明出处 http://www.cnblogs.com/pretty-guy/p/8268745.html
下一篇博客打算介绍利用CADisplayLink与CoreGraphics结合实现动画
iOS 动画篇 (二) CAShapeLayer与CoreAnimation结合使用的更多相关文章
- iOS 动画笔记 (二)
有它们俩你就够了! 说明:下面有些概念我说的不怎么详细,网上实在是太多了,说了我觉得也意义不大了!但链接都给大家了,可以自己去看,重点梳理学习写动画的一个过程和一些好的博客! 一:说说这两个三方库,C ...
- iOS动画篇:UIView动画
iOS的动画效果一直都很棒很,给人的感觉就是很炫酷很流畅,起到增强用户体验的作用.在APP开发中实现动画效果有很多种方式,对于简单的应用场景,我们可以使用UIKit提供的动画来实现. UIView动画 ...
- iOS 动画篇 之 Core Animation (一)
iOS中实现动画有两种方式,一种是自己不断的通过drawRect:方法来绘制,另外一种就是使用核心动画(Core Animation). 导语: 核心动画提供高帧速率和流畅的动画,而不会增加CPU的负 ...
- iOS 动画篇 (三) CADisplayLink与CoreGraphics实现动画
本文主要介绍利用CoreGraphics和CADisplayLink来实现一个注水动画.来一个效果图先: 在介绍注水动画前,先介绍利用CoreGraphics实现进度条的绘制. 一.扇形进度绘制 效果 ...
- iOS动画篇:核心动画
转:http://www.cocoachina.com/ios/20160517/16290.html 基本概念 1.什么是核心动画 Core Animation(核心动画)是一组功能强大.效果华丽的 ...
- iOS 动画笔记 (一)
你也肯定喜欢炫酷的动画! 在APP中,动画就是一个点睛之笔!可以给用户增加一些独特的体验感,估计也有许多的和我一样的,看着那些觉得不错的动画,也就只能流口水的孩子,毕竟可能不知道从哪里下手去写!动画学 ...
- iOS动画学习
学习一下动画,感谢以下大神的文章: UIView:基础动画.关键帧动画.转场动画 Core Animation :基础动画,关键帧动画,动画组,转场动画,逐帧动画 CALayer :CALaye ...
- ios 动画学习的套路 (二)
有它们俩你就够了! 说明:下面有些概念我说的不怎么详细,网上实在是太多了,说了我觉得也意义不大了!但链接都给大家了,可以自己去看,重点梳理学习写动画的一个过程和一些好的博客! (一) 说说这两个三方库 ...
- iOS 动画基础总结篇
iOS 动画基础总结篇 动画的大体分类(个人总结可能有误) 分类.png UIView 动画 属性动画 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...
随机推荐
- js函数的作用域与this指向
函数的作用域与this指向是js中很重要的一部分,理清这点东西需要个逻辑,看看我的逻辑怎么样... 下面是个提纲,可以直接挑你感兴趣的条目阅读. 函数的定义方式:直接定义(window下,内部定义), ...
- HDU 4911 Inversion 树状数组求逆序数对
显然每次交换都能降低1 所以求出逆序数对数,然后-=k就好了.. . _(:зゝ∠)_ #include<stdio.h> #include<string.h> #includ ...
- Django项目实践4 - Django网站管理(后台管理员)
http://blog.csdn.net/pipisorry/article/details/45079751 上篇:Django项目实践3 - Django模型 Introduction 对于某一类 ...
- [源码]解析 SynchronousQueue 上界,下界.. 数据保存和数据传递. 堵塞队列. 有无频繁await?
简析SynchronousQueue.LinkedBlockingQueue(两个locker,更快),ArrayBlockingQueue(一个locker,读写都竞争) 三者都是bloc ...
- Hive修改行级别数据
我们知道Hive0.14版本之前是不支持行级别的插入,更新,删除的,0.14版本之后可以通过修改相关配置得以支持,但是在不修改默认配置的情况下是不是完全没有办法呢?不是的,这里有个比较简单的方法,前提 ...
- 通过案例学习 Secret - 每天5分钟玩转 Docker 容器技术(110)
在下面的例子中,我们会部署一个 WordPress 应用,WordPress 是流行的开源博客系统. 我们将创建一个 MySQL service,将密码保存到 secret 中.我们还会创建一个 Wo ...
- ansible编译httpd playbook示例
以下是playbook的内容.它的处理流程是: 1.先在本地下载apr,apr-util,httpd共3个.tar.gz文件. 2.解压这3个文件. 3.安装pcre和pcre-devel依赖包. 4 ...
- MyBatis_查询缓存01
一.查询缓存 查询缓存的使用,主要是为了提高查询访问速度.将用户对同一数据的重复查询过程简单化,不在每次均从数据库中查询获取结果数据,从而提高访问速度. MyBatis的查询缓存机制,根据缓存区的作用 ...
- 【TEGer 在全球架构师峰会】 : 腾讯海外计费系统架构演进
欢迎大家前往云加社区,获取更多腾讯海量技术实践干货哦~ 作者简介:abllen,2008年加入腾讯,一直专注于腾讯计费平台建设,主导参与了腾讯充值中心.计费开放平台.统一计费米大师等项目,见证了米大师 ...
- 轨迹系列——Socket总结及实现基于TCP或UDP的809协议方法
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 在上一篇博客中我详细介绍了809协议的内容.809协议规范了通 ...