服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - 总结iOS实现渐变颜色的三种方法

总结iOS实现渐变颜色的三种方法

2021-02-03 15:42iOS开发网 IOS

这篇文章主要给大家总结了iOS实现渐变颜色的三种方法,分别是利用CAGradientLayer实现渐变、Core Graphics相关方法实现渐变以及用CAShapeLayer作为layer的mask属性实现,大家可以根据自己的需要选择使用,下面来一起看看吧。

在ios开发过程中有的时候会需要用到渐变颜色,这篇文章总结了三种方法来实现,有需要的朋友们下面来一起看看吧。

一、cagradientlayer实现渐变

cagradientlayer是calayer的一个特殊子类,用于生成颜色渐变的图层,使用较为方便

下面介绍下它的相关属性:

      colors 渐变的颜色

      locations 渐变颜色的分割点

      startpoint&endpoint 颜色渐变的方向,范围在(0,0)与(1.0,1.0)之间,如(0,0)(1.0,0)代表水平方向渐变,(0,0)(0,1.0)代表竖直方向渐变

?
1
2
3
4
5
6
7
cagradientlayer *gradientlayer = [cagradientlayer layer];
gradientlayer.colors = @[(__bridge id)[uicolor redcolor].cgcolor, (__bridge id)[uicolor yellowcolor].cgcolor, (__bridge id)[uicolor bluecolor].cgcolor];
gradientlayer.locations = @[@0.3, @0.5, @1.0];
gradientlayer.startpoint = cgpointmake(0, 0);
gradientlayer.endpoint = cgpointmake(1.0, 0);
gradientlayer.frame = cgrectmake(0, 100, 300, 100);
[self.view.layer addsublayer:gradientlayer];

总结iOS实现渐变颜色的三种方法

cagradientlayer实现渐变标间简单直观,但存在一定的局限性,比如无法自定义整个渐变区域的形状,如环形、曲线形的渐变。

二、core graphics相关方法实现渐变

ios core graphics中有两个方法用于绘制渐变颜色,cgcontextdrawlineargradient可以用于生成线性渐变,cgcontextdrawradialgradient用于生成圆半径方向颜色渐变。函数可以自定义path,无论是什么形状都可以,原理都是用来做clip,所以需要在cgcontextclip函数前调用cgcontextaddpath函数把cgpathref加入到context中。
另外一个需要注意的地方是渐变的方向,方向是由两个点控制的,点的单位就是坐标。因此需要正确从cgpathref中找到正确的点,方法当然有很多种看具体实现,本例中,我就是简单得通过调用cgpathgetboundingbox函数,返回cgpathref的矩形区域,然后根据这个矩形取两个点,读者可以根据自行需求修改具体代码。

1-> 线性渐变

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
- (void)drawlineargradient:(cgcontextref)context
  path:(cgpathref)path
 startcolor:(cgcolorref)startcolor
  endcolor:(cgcolorref)endcolor
{
 cgcolorspaceref colorspace = cgcolorspacecreatedevicergb();
 cgfloat locations[] = { 0.0, 1.0 };
 
 nsarray *colors = @[(__bridge id) startcolor, (__bridge id) endcolor];
 
 cggradientref gradient = cggradientcreatewithcolors(colorspace, (__bridge cfarrayref) colors, locations);
 
 
 cgrect pathrect = cgpathgetboundingbox(path);
 
 //具体方向可根据需求修改
 cgpoint startpoint = cgpointmake(cgrectgetminx(pathrect), cgrectgetmidy(pathrect));
 cgpoint endpoint = cgpointmake(cgrectgetmaxx(pathrect), cgrectgetmidy(pathrect));
 
 cgcontextsavegstate(context);
 cgcontextaddpath(context, path);
 cgcontextclip(context);
 cgcontextdrawlineargradient(context, gradient, startpoint, endpoint, 0);
 cgcontextrestoregstate(context);
 
 cggradientrelease(gradient);
 cgcolorspacerelease(colorspace);
}
 
- (void)viewdidload
{
 [super viewdidload];
 // do any additional setup after loading the view.
 
 //创建cgcontextref
 uigraphicsbeginimagecontext(self.view.bounds.size);
 cgcontextref gc = uigraphicsgetcurrentcontext();
 
 //创建cgmutablepathref
 cgmutablepathref path = cgpathcreatemutable();
 
 //绘制path
 cgrect rect = cgrectmake(0, 100, 300, 200);
 cgpathmovetopoint(path, null, cgrectgetminx(rect), cgrectgetminy(rect));
 cgpathaddlinetopoint(path, null, cgrectgetmidx(rect), cgrectgetmaxy(rect));
 cgpathaddlinetopoint(path, null, cgrectgetwidth(rect), cgrectgetmaxy(rect));
 cgpathclosesubpath(path);
 
 //绘制渐变
 [self drawlineargradient:gc path:path startcolor:[uicolor greencolor].cgcolor endcolor:[uicolor redcolor].cgcolor];
 
 //注意释放cgmutablepathref
 cgpathrelease(path);
 
 //从context中获取图像,并显示在界面上
 uiimage *img = uigraphicsgetimagefromcurrentimagecontext();
 uigraphicsendimagecontext();
 
 uiimageview *imgview = [[uiimageview alloc] initwithimage:img];
 [self.view addsubview:imgview];
}

总结iOS实现渐变颜色的三种方法

2-> 圆半径方向渐变

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
- (void)drawradialgradient:(cgcontextref)context
  path:(cgpathref)path
 startcolor:(cgcolorref)startcolor
  endcolor:(cgcolorref)endcolor
{
 cgcolorspaceref colorspace = cgcolorspacecreatedevicergb();
 cgfloat locations[] = { 0.0, 1.0 };
 
 nsarray *colors = @[(__bridge id) startcolor, (__bridge id) endcolor];
 
 cggradientref gradient = cggradientcreatewithcolors(colorspace, (__bridge cfarrayref) colors, locations);
 
 
 cgrect pathrect = cgpathgetboundingbox(path);
 cgpoint center = cgpointmake(cgrectgetmidx(pathrect), cgrectgetmidy(pathrect));
 cgfloat radius = max(pathrect.size.width / 2.0, pathrect.size.height / 2.0) * sqrt(2);
 
 cgcontextsavegstate(context);
 cgcontextaddpath(context, path);
 cgcontexteoclip(context);
 
 cgcontextdrawradialgradient(context, gradient, center, 0, center, radius, 0);
 
 cgcontextrestoregstate(context);
 
 cggradientrelease(gradient);
 cgcolorspacerelease(colorspace);
}
 
- (void)viewdidload
{
 [super viewdidload];
 // do any additional setup after loading the view.
 
 //创建cgcontextref
 uigraphicsbeginimagecontext(self.view.bounds.size);
 cgcontextref gc = uigraphicsgetcurrentcontext();
 
 //创建cgmutablepathref
 cgmutablepathref path = cgpathcreatemutable();
 
 //绘制path
 cgrect rect = cgrectmake(0, 100, 300, 200);
 cgpathmovetopoint(path, null, cgrectgetminx(rect), cgrectgetminy(rect));
 cgpathaddlinetopoint(path, null, cgrectgetmidx(rect), cgrectgetmaxy(rect));
 cgpathaddlinetopoint(path, null, cgrectgetwidth(rect), cgrectgetmaxy(rect));
 cgpathaddlinetopoint(path, null, cgrectgetwidth(rect), cgrectgetminy(rect));
 cgpathclosesubpath(path);
 
 //绘制渐变
 [self drawradialgradient:gc path:path startcolor:[uicolor greencolor].cgcolor endcolor:[uicolor redcolor].cgcolor];
 
 //注意释放cgmutablepathref
 cgpathrelease(path);
 
 //从context中获取图像,并显示在界面上
 uiimage *img = uigraphicsgetimagefromcurrentimagecontext();
 uigraphicsendimagecontext();
 
 uiimageview *imgview = [[uiimageview alloc] initwithimage:img];
 [self.view addsubview:imgview];
}

总结iOS实现渐变颜色的三种方法

三、以cashapelayer作为layer的mask属性

calayer的mask属性可以作为遮罩让layer显示mask遮住(非透明)的部分;cashapelayer为calayer的子类,通过path属性可以生成不同的形状,将cashapelayer对象用作layer的mask属性的话,就可以生成不同形状的图层。

故生成颜色渐变有以下几个步骤:

     1、生成一个imageview(也可以为layer),image的属性为颜色渐变的图片

     2、生成一个cashapelayer对象,根据path属性指定所需的形状

     3、将cashapelayer对象赋值给imageview的mask属性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
- (void)viewdidload
{
 [super viewdidload];
 
 [self.view addsubview:self.firstcircle];
 _firstcircle.frame = cgrectmake(0, 0, 200, 200);
 _firstcircle.center = cgpointmake(cgrectgetwidth(self.view.bounds) / 2.0, cgrectgetheight(self.view.bounds) / 2.0);
 cgfloat firscirclewidth = 5;
 self.firstcircleshapelayer = [self generateshapelayerwithlinewidth:firscirclewidth];
 _firstcircleshapelayer.path = [self generatebezierpathwithcenter:cgpointmake(100, 100) radius:100].cgpath;
 _firstcircle.layer.mask = _firstcircleshapelayer;
}
 
- (cashapelayer *)generateshapelayerwithlinewidth:(cgfloat)linewidth
{
 cashapelayer *waveline = [cashapelayer layer];
 waveline.linecap = kcalinecapbutt;
 waveline.linejoin = kcalinejoinround;
 waveline.strokecolor = [uicolor redcolor].cgcolor;
 waveline.fillcolor = [[uicolor clearcolor] cgcolor];
 waveline.linewidth = linewidth;
 waveline.backgroundcolor = [uicolor clearcolor].cgcolor;
 
 return waveline;
}
 
- (uibezierpath *)generatebezierpathwithcenter:(cgpoint)center radius:(cgfloat)radius
{
 uibezierpath *circlepath = [uibezierpath bezierpathwitharccenter:center radius:radius startangle:0 endangle:2*m_pi clockwise:no];
 
 return circlepath;
}
 
- (uiimageview *)firstcircle
{
 if (!_firstcircle) {
 self.firstcircle = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"circlebackground"]];
 _firstcircle.layer.maskstobounds = yes;
 _firstcircle.alpha = 1.0;
 }
 
 return _firstcircle;
}

总结iOS实现渐变颜色的三种方法

总结

以上就是这篇文章的全部内容了,希望本文的内容对各位ios开发者们能有所帮助,如果有疑问大家可以留言交流。

延伸 · 阅读

精彩推荐
  • IOSiOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。...

    张无忌!4812021-03-09
  • IOSiOS APP实现微信H5支付示例总结

    iOS APP实现微信H5支付示例总结

    这篇文章主要介绍了iOS APP实现微信H5支付示例总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    一张小A11332021-06-01
  • IOSiOS常见的几个修饰词深入讲解

    iOS常见的几个修饰词深入讲解

    这篇文章主要给大家介绍了关于iOS常见的几个修饰词的相关资料,iOS修饰词包括assign、weak、strong、retain、copy、nonatomic、atomic、readonly、readwrite,文中通过示...

    郡王丶千夜7422021-05-10
  • IOSiOS10 Xcode8适配7个常见问题汇总

    iOS10 Xcode8适配7个常见问题汇总

    这篇文章主要为大家详细汇总了iOS10 Xcode8适配7个常见问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    索马里猫10332021-02-01
  • IOS谈一谈iOS单例模式

    谈一谈iOS单例模式

    这篇文章主要和大家谈一谈iOS中的单例模式,单例模式是一种常用的软件设计模式,想要深入了解iOS单例模式的朋友可以参考一下...

    彭盛凇11872021-01-19
  • IOSIOS网络请求之AFNetWorking 3.x 使用详情

    IOS网络请求之AFNetWorking 3.x 使用详情

    本篇文章主要介绍了IOS网络请求之AFNetWorking 3.x 使用详情,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    总李写代码6892021-03-04
  • IOSiOS逆向教程之logify跟踪方法的调用

    iOS逆向教程之logify跟踪方法的调用

    这篇文章主要给大家介绍了关于iOS逆向教程之logify跟踪方法调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学...

    Mr.Guo11472021-04-28
  • IOSxcode8提交ipa失败无法构建版本问题的解决方案

    xcode8提交ipa失败无法构建版本问题的解决方案

    xcode升级到xcode8后发现构建不了新的版本。怎么解决呢?下面小编给大家带来了xcode8提交ipa失败无法构建版本问题的解决方案,非常不错,一起看看吧...

    Cinna丶7542021-02-03