CAShapeLayer--备用
之前讲过CALayer动画相关知识,再来看看更加复杂的CAShapeLayer相关的动画知识.
普通CALayer在被初始化时是需要给一个frame值的,这个frame值一般都与给定view的bounds值一致,它本身是有形状的,而且是矩形.
CAShapeLayer在初始化时也需要给一个frame值,但是,它本身没有形状,它的形状来源于你给定的一个path,然后它去取CGPath值,它与CALayer有着很大的区别
CAShapeLayer有着几点很重要:
1. 它依附于一个给定的path,必须给与path,而且,即使path不完整也会自动首尾相接
2. strokeStart以及strokeEnd代表着在这个path中所占用的百分比
3. CAShapeLayer动画仅仅限于沿着边缘的动画效果,它实现不了填充效果
以下给出如何使用CAShapeLayer
// 创建一个view
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:showView];
showView.backgroundColor = [UIColor redColor];
showView.alpha = 0.5;
// 贝塞尔曲线(创建一个圆)
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100 / 2.f, 100 / 2.f)
radius:100 / 2.f
startAngle:0
endAngle:M_PI * 2
clockwise:YES];
// 创建一个shapeLayer
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = showView.bounds; // 与showView的frame一致
layer.strokeColor = [UIColor greenColor].CGColor; // 边缘线的颜色
layer.fillColor = [UIColor clearColor].CGColor; // 闭环填充的颜色
layer.lineCap = kCALineCapSquare; // 边缘线的类型
layer.path = path.CGPath; // 从贝塞尔曲线获取到形状
layer.lineWidth = 4.0f; // 线条宽度
layer.strokeStart = 0.0f;
layer.strokeEnd = 0.1f;
// 将layer添加进图层
[showView.layer addSublayer:layer];
// 3s后执行动画操作(直接赋值就能产生动画效果)
[[GCDQueue mainQueue] execute:^{
layer.speed = 0.1;
layer.strokeStart = 0.5;
layer.strokeEnd = 0.9f;
layer.lineWidth = 1.0f;
} afterDelay:NSEC_PER_SEC * 3];
// 给这个layer添加动画效果
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 1.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.5f];
pathAnimation.toValue = [NSNumber numberWithFloat:0.8f];
[layer addAnimation:pathAnimation forKey:nil];
// 创建一个gradientLayer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = showView.bounds;
[gradientLayer setColors:[NSArray arrayWithObjects:
(id)[[UIColor redColor] CGColor],
(id)[[UIColor yellowColor] CGColor], nil]];
[gradientLayer setLocations:@[@0.5,@0.9,@1]];
[gradientLayer setStartPoint:CGPointMake(0.5, 1)];
[gradientLayer setEndPoint:CGPointMake(0.5, 0)];
附录:
TestView.h

#import <UIKit/UIKit.h>
@interface TestView : UIView
{
CAShapeLayer *layer;
}
- (void)strokeStart:(CGFloat)value;
- (void)strokeEnd:(CGFloat)value;
@end

TestView.m

#import "TestView.h" @implementation TestView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
layer = [CAShapeLayer layer];
layer.frame = self.bounds; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.height / 2.0f,
self.frame.size.height / 2.0f)
radius:self.frame.size.height / 2.f
startAngle:0
endAngle:M_PI * 2
clockwise:YES]; layer.strokeColor = [UIColor greenColor].CGColor; // 边缘线的颜色
layer.fillColor = [UIColor clearColor].CGColor; // 闭环填充的颜色
layer.lineCap = kCALineCapSquare; // 边缘线的类型
layer.path = path.CGPath; // 从贝塞尔曲线获取到形状
layer.lineWidth = 1.0f; // 线条宽度
layer.strokeStart = 0.0f;
layer.strokeEnd = 0.0f; [self.layer addSublayer:layer];
}
return self;
} - (void)strokeStart:(CGFloat)value
{
layer.speed = 1;
layer.strokeStart = value;
} - (void)strokeEnd:(CGFloat)value
{
layer.speed = 1;
layer.strokeEnd = value;
} @end
CAShapeLayer--备用的更多相关文章
- Windows2012R2备用域控搭建
Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自己的ip,备dns:备域控的ip备域控的主dns:自己的ip,备dns:主域控的ip 客户端主dns:主域控的ip,备dns: ...
- 用CAShapeLayer实现一个简单的饼状图(PieView)
自己写了一个简单的PieView,demo在这里:https://github.com/Phelthas/LXMPieView 效果如图: 参考了https://github.com/kevinzho ...
- 关于CAShapeLayer的一些实用案例和技巧【转】
本文授权转载,作者:@景铭巴巴 一.使用CAShapeLayer实现复杂的View的遮罩效果 1.1.案例演示 最近在整理一个聊天的项目的时候,发送图片的时候,会有一个三角的指向效果,指向这张图片的发 ...
- 动画黄金搭档:CADisplayLink&CAShapeLayer
我们在开发中有时会遇到一些看似非常复杂的动画,不知该如何下手,今天的这篇文章中我会讲到如何利用CADisplayLink和CAShapeLayer来构建一些复杂的动画,希望能在你下次构建动画中,给你一 ...
- 动画黄金搭档:CADisplayLink & CAShapeLayer
我们在开发中有时会遇到一些看似非常复杂的动画,不知该如何下手,今天的这篇文章中我会讲到如何利用CADisplayLink和CAShapeLayer来构建一些复杂的动画,希望能在你下次构建动画中,给你一 ...
- 图的割点 | | jzoj【P1230】 | | gdoi | |备用交换机
写在前面:我真的不知道图的割点是什么.... 看见ftp图论专题里面有个dfnlow的一个文档,于是怀着好奇的心情打开了这个罪恶的word文档,,然后就开始漫长的P1230的征讨战.... 图的割点是 ...
- 如何使用代码或脚本启用SharePoint的备用语言
SP的多语言,需要安装语言包,然后手工去sharepoint下启动备用语言,如下图: [网站操作]-[语言设置]: 方法一:采用powershell处理 在很多项目情况下,需要用代码进行备用语言启动. ...
- iOS关于CAShapeLayer与UIBezierPath的知识内容
使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 . 1:UIBezierPath: UIBezierPath是在 UIKit 中 ...
- js 处理日期 看着比较全,备用
http://www.cnblogs.com/endora/archive/2012/12/06/endorahe.html js 处理日期 看着比较全,备用
- iOS CAShapeLayer记录
基本知识 看看官方说明: /* The shape layer draws a cubic Bezier spline in its coordinate space. * * The spline ...
随机推荐
- MySQL(3):数据库操作
1.创建数据库: 数据定义语言(DDL): create database db_name[数据库选项] 注:数据库命名规则:大小写取决于当前操作系统,见名知意,推荐下划线 标识符的字符: 使用任意字 ...
- jemalloc源码结构分析(一):内存申请处理过程
一.5种malloc方法 1)tcache_alloc_small 2)arena_malloc_small 3)tcache_alloc_large 4)arena_malloc_large 5)h ...
- jQuery 黑白插件
1 add jQuery and plug in to the page <script src="js/jquery.min.js"></script> ...
- akka构建简单分布式应用
http://www.cnblogs.com/hequn/articles/3764630.html 当程序的要求达到一台计算机的极限时,我们便需要将程序分布式化,让程序运行在多台计算机上.akka提 ...
- 第六篇、CSS属性
<!--1.可继承性 visible(可见的):hidden --掩藏,但是结构还保存 cursor(光标样式):pointer(手指)crosshair(十字架) 一般是文字控制属性 内联标签 ...
- C# ACM poj1002
排序 public static void acm1002(string[] azx) { string[] a = new string[azx.Length]; ; i < azx.Leng ...
- 学习之spring注解DI疑惑
接口定义 package com; public interface IPaly { void say(); } 接口实现类 package com; import org.springframewo ...
- WPF 程序中启动和关闭外部.exe程序
当需要在WPF程序启动时,启动另一外部程序(.exe程序)时,可以按照下面的例子来: C#后台代码如下: using System; using System.Collections.Generic; ...
- (poj)1064 Cable master 二分+精度
题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...
- Windows Phone 动态改变ListBox样式
使用ListBox时通常会借助ItemTemplate帮助我们实现更复杂多样的样式显示,体现了Xaml的灵活.如何动态改变变ListBox的样式,实现类似电脑资源管理器中列表显示和图标显示形式的替换. ...