主要思路是:
1.使用UIBezierPath绘制一个六边形路径
2.创建一个CAShapeLayer图层,将这个六边形path设置到CAShapeLayer属性上。然后设置fillColor为透明,strokeColor为黑色,lineWidth为5.0
3.创建一个CAGradientLayer渐变色图层,并将它的渐变类型设置成kCAGradientLayerConic以圆心为起点,以圆边为中点,并沿着圆旋转一周
4.设置CAGradientLayer.mask = CAShapeLayer, 得出一个被CAShapeLayer切割后的六边形
5.给CAGradientLayer的colors内容添加动态,让渐变图层的内容旋转,从而得到六边形渐变色旋转的结果
创建一个路径,也可以根据自己的需要进行创建,这里创建的是一个六边形
CGFloat squareSize = MIN(imageSize.width, imageSize.height);
UIBezierPath * path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (squareSize / 2), (squareSize / 4))];
[path addLineToPoint:CGPointMake((squareSize / 2), 0)];
[path addLineToPoint:CGPointMake(squareSize - ((sin(M_1_PI / 180 * 60)) * (squareSize / 2)), (squareSize / 4))];
[path addLineToPoint:CGPointMake(squareSize - ((sin(M_1_PI / 180 * 60)) * (squareSize / 2)), (squareSize / 2) + (squareSize / 4))];
[path addLineToPoint:CGPointMake((squareSize / 2), squareSize)];
[path addLineToPoint:CGPointMake((sin(M_1_PI / 180 * 60)) * (squareSize / 2), (squareSize / 2) + (squareSize / 4))];
[path closePath];
将创建好的UIBezierPath传递给CAShapeLayer图层中,让图层拥有path的图案
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.frame = CGRectMake(0, 5, self.bounds.size.width, self.bounds.size.height);
pathLayer.path = path.CGPath;
pathLayer.strokeColor = [UIColor blackColor].CGColor;
pathLayer.fillColor = [UIColor clearColor].CGColor;
pathLayer.lineWidth = 5.0;
pathLayer.lineJoin = kCALineJoinRound;
创建一个渐变色图层
// 创建渐变图层
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.bounds;
gradientLayer.type = kCAGradientLayerConic;
// 定义颜色数组,中间白色,两边透明
NSArray *colors = @[(id)[UIColor colorWithWhite:1 alpha:1].CGColor,
(id)[UIColor colorWithWhite:0.8 alpha:0.8].CGColor,
(id)[UIColor colorWithWhite:0.6 alpha:0.6].CGColor,
(id)[UIColor colorWithWhite:0.5 alpha:0].CGColor,
(id)[UIColor colorWithWhite:0 alpha:0].CGColor,
(id)[UIColor colorWithWhite:0.5 alpha:0].CGColor,
(id)[UIColor colorWithWhite:0.6 alpha:0.6].CGColor,
(id)[UIColor colorWithWhite:0.8 alpha:0.8].CGColor,
(id)[UIColor colorWithWhite:1 alpha:1].CGColor];
gradientLayer.colors = colors;
gradientLayer.startPoint = CGPointMake(0.5, 0.5);
gradientLayer.endPoint = CGPointMake(0, 0.5);
// 设置颜色位置
gradientLayer.locations = @[@0.0, @0.05, @0.1, @0.15, @0.5, @0.85, @0.9, @0.95, @1.0];
将渐变图层添加到View的图层上,然后使用Shape图层进行mask切割,得出想要的图案。
mask的的原理:对应设置的图层中,如果像素是黑色的,其对应的下面的图层内容就显示,如果某部分图层像素是透明的,那么透明像素下面的图层内容就不展示。
[self.layer addSublayer:self.gradientLayer];
self.gradientLayer.mask = self.pathLayer;
开启渐变图层内容动画
将图层中的endPoint随时间进行线性移动,从而实现图层固定,里面的内容随时间进行动画。
- (void)createTimer {
gcdTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
dispatch_source_set_timer(gcdTimer, DISPATCH_TIME_NOW, 0.02 * NSEC_PER_SEC, 0.01 * NSEC_PER_SEC);
dispatch_source_set_event_handler(gcdTimer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self handleTimerEvent];
});
});
dispatch_resume(gcdTimer);
} - (void)handleTimerEvent {
CGFloat speed = 0.04;
CGPoint endPoint = self.gradientLayer.endPoint;
if (endPoint.x <= 0 && endPoint.y > 0 && !self.leftMove) {
self.bottomMove = YES;
self.topMove = NO;
self.gradientLayer.endPoint = CGPointMake(endPoint.x, endPoint.y-speed);
} else if (endPoint.x < 1 && endPoint.y <= 0 && !self.topMove) {
self.leftMove = YES;
self.rightMove = NO;
self.gradientLayer.endPoint = CGPointMake(endPoint.x+speed, endPoint.y);
} else if (endPoint.x >= 1 && endPoint.y < 1 && !self.rightMove) {
self.topMove = YES;
self.bottomMove = NO;
self.gradientLayer.endPoint = CGPointMake(endPoint.x, endPoint.y+speed);
} else if (endPoint.x >= 0 && endPoint.y >= 1 && !self.bottomMove) {
self.rightMove = YES;
self.leftMove = NO;
self.gradientLayer.endPoint = CGPointMake(endPoint.x-speed, endPoint.y);
}
}
最终得到一个六边形,它的边框颜色是渐变色,并且这个渐变色沿着六边形旋转。

使用CAShapeLayer,UIBezierPath,CAGradientLayer构建边框颜色会旋转的六边形的更多相关文章

  1. 百度编辑器ueditor插入表格没有边框颜色的解决方法

    附:从word excel 中 复制的表格提交后无边框,参考这个同学的,写的很详细:   http://blog.csdn.net/lovelyelfpop/article/details/51678 ...

  2. input 边框颜色

    border 的属性 有三个 border:5px solid red; 如果上述值缺少一个没有关系,例如border:#FF0000;是允许的. 分别对应:border-width, border- ...

  3. winform设置button的边框颜色,或取消边框颜色,不显示边框

    // winform设置边框颜色不像webform那么简单,可以通过设置FlatAppearance,也可以通过重绘实现. 一.设置按钮本身属性 buttonBubufx.FlatStyle = Fl ...

  4. UIButton设置圆角和边框及边框颜色

    1. 按钮边框颜色 //设置边框颜色 [btn.layer setMasksToBounds:YES]; [btn.layer setCornerRadius:10.0]; //设置矩形四个圆角半径 ...

  5. JAVA swing中JPanel如何实现分组框的效果以及设置边框颜色 分类: Java Game 2014-08-16 12:21 198人阅读 评论(0) 收藏

    代码如下: import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridLayout; import javax.sw ...

  6. 为UITextView添加与UITextField一样的边框——UITextField默认边框颜色、宽度、圆角

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3789052.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  7. HTML快速入门5——不规则表格、表格背景、边框颜色

    转自:http://blog.csdn.net/ysuncn/article/details/2214153 不规则表格 例子: <table border=1><tr>< ...

  8. Winform改变Textbox边框颜色(转)

    namespace MyTextBoxOne { //使用时必须把文本框的BorderStyle为FixedSingle才能使用 //一些控件(如TextBox.Button等)是由系统进程绘制,重载 ...

  9. 鼠标点击 input,显示瞬间的边框颜色,对之修改与隐藏

    示例的是项目中的遇到的,要做成的效果是点击该图片按钮,达到切换图片的效果: HTML代码如下: <input class="dBox3Ulimg" type="im ...

  10. html中如何修改选中 用input做的搜索框 的边框颜色

    html中如何修改选中 用input做的搜索框 的边框颜色 如图,当我鼠标选中输入框时,内边框会变成蓝色 我的问题是: 1.如何把蓝色去掉? 2.如何改成别的颜色? 首先感谢 UI设计师提出的需求,解 ...

随机推荐

  1. 2018年第九届 蓝桥杯A组 C/C++决赛题解

    蓝桥杯历年国赛真题汇总:Here 1.三角形面积 已知三角形三个顶点在直角坐标系下的坐标分别为: (2.3, 2.5) (6.4, 3.1) (5.1, 7.2) 求该三角形的面积. 注意,要提交的是 ...

  2. C++函数:std::tie 详解

    在补CF周赛时发现dalao用了一个 tie函数和tuple类型,表示没怎么接触,现在稍微学习记录一下. tuple 即元组,可以理解为pair的扩展,可以用来将不同类型的元素存放在一起,常用于函数的 ...

  3. 基于 Serverless+OSS 分分钟实现图片秒变素描

    简介: 在阿里云Serverless函数计算服务中部署普通图片转素描图函数服务,实现将批量上传到指定OSS桶内的图片自动转换为素描图并保存到另一个OSS桶内 场景介绍 小明接到学校老师安排的任务,需要 ...

  4. Redis无法使用ip访问

    问题:    启动redis服务,可以使用127.0.0.1配置并使用访问redis,但是换成IP地址就无法访问 127.0.0.1 可以 [root]#  ./redis-cli -c -h 127 ...

  5. S3C2440移植uboot之编译烧写uboot

    目录 移植环境 获取uboot 更新交叉编译工具 配置环境变量 移植环境 主 机:VMWare--ubuntu16.04 开发板:S3C2440 编译器:arm-linux-gcc-4.3.2.tgz ...

  6. vue2.x封装svg组件并使用

    https://blog.csdn.net/ChickenBro_/article/details/134027803

  7. SV 自定义数据类型

    概述 自定义类型 枚举类型 定义枚举值 自定义枚举类型 枚举类型之间进行赋值是可以的 枚举类型可以赋值给整型,整型不能直接赋值给枚举类型 枚举类型 + 1 ==> 会进行隐式的转换,枚举类型转换 ...

  8. 缓存选型:Redis or MemCache

    ★ Redis24篇集合 1 背景 互联网产品为了保证高性能和高可用性,经常会使用缓存来进行架构设计.最常用的就是使用Redis了,也有部分企业会选择使用Memcache. 所以了解 Redis 和 ...

  9. 【C】《C专家编程》阅读体会

     [来源]https://mp.weixin.qq.com/s/0kmN5knql4yrOuUcnebwIQ

  10. 【MicroPython] 用 c 添加接口 -- 添加 module

    [来源]https://www.eemaker.com/micropython-add-module.html