Demo

使用效果//包含头文件UIView+Gradient.h

[self.label setGradientBackgroundWithColors:@[[UIColor redColor],[UIColor orangeColor]] locations:nil startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];

[self.btn setGradientBackgroundWithColors:@[[UIColor redColor],[UIColor orangeColor]] locations:nil startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];

[self.tempView setGradientBackgroundWithColors:@[[UIColor redColor],[UIColor orangeColor]] locations:nil startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];

常规方案

说起颜色渐变一般来说有两种实现方式,一是使用CAGradientLayer而是使用Core Graphics的相关方法,具体可参考ios实现颜色渐变的几种方法。

问题

CAGradientLayer可以说是实现起来比较方便的一个方案了,但是对于Autolayout来说,我们需要更新CAGradientLayer的frame,这就得增加不少代码,而且代码会散布在其他方法中,这就不是很友好了。

优化方案

原理

我们知道UIView显示的内容实际是绘制在CALayer上的,默认情况下创建一个UIView时会创建一个CALayer作为UIView的root layer,那么如果我们直接把这个root layer替换成CAGradientLayer就能直接实现渐变效果,而且跟随UIView的frame变化。

那么如何替换呢?UIView有个+layerClass类方法,官方描述:Returns the class used to create the layer for instances of this class.

This method returns the CALayer class object by default. Subclasses can override this method and return a different layer class as needed. For example, if your view uses tiling to display a large scrollable area, you might want to override this method and return the CATiledLayer class.

This method is called only once early in the creation of the view in order to create the corresponding layer object.

大概意思就是系统默认会返回CALayer类,但是如果你重写了这个类方法,那么就会返你return的类,从而创建你需要的layer。

在实际实现这个分类的时候发现+layerClass有点类似+load方法,即只要文件在项目中,就会调用该方法,不需要显式包含头文件。

实现

我们创建一个UIView的分类UIView+Gradient,利用runtime添加CAGradientLayer的一些属性以及设置背景色的方法,如下://UIView+Gradient.h

@property(nullable, copy) NSArray *colors;

@property(nullable, copy) NSArray *locations;

@property CGPoint startPoint;

@property CGPoint endPoint;

+ (UIView *_Nullable)gradientViewWithColors:(NSArray *_Nullable)colors locations:(NSArray *_Nullable)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint;

- (void)setGradientBackgroundWithColors:(NSArray *_Nullable)colors locations:(NSArray *_Nullable)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint;

然后在.m文件中对重写类方法+layerClass以及实现相关设置://UIView+Gradient.m

+ (Class)layerClass {

return [CAGradientLayer class];

}

+ (UIView *)gradientViewWithColors:(NSArray *)colors locations:(NSArray *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {

UIView *view = [[self alloc] init];

[view setGradientBackgroundWithColors:colors locations:locations startPoint:startPoint endPoint:endPoint];

return view;

}

- (void)setGradientBackgroundWithColors:(NSArray *)colors locations:(NSArray *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {

NSMutableArray *colorsM = [NSMutableArray array];

for (UIColor *color in colors) {

[colorsM addObject:(__bridge id)color.CGColor];

}

self.colors = [colorsM copy];

self.locations = locations;

self.startPoint = startPoint;

self.endPoint = endPoint;

}

然后就能像最开始提到的那样轻松的一句话设置渐变背景色了。

注意事项

大部分View在设置colors属性后原有的backgroundColor属性会失效,即不管backgroundColor设置的是什么颜色,都会显示渐变色,要显示正常的backgroundColor只需要将colors设置成nil。// colors优先级高于backgroundColor的View

UIView

UIButton

UIImageView

UITextView

UITextField

UISlider

UIStepper

UISwitch

UISegmentedControl

在实测中发现UILabel在设置colors后还是会显示backgroundColor,要显示渐变色需要将backgroundColor设置为clearColor。

虽然在UIView的分类中重写了+layerClass,但是有可能存在一些View 已经重写了+layerClass,那么就有可能该View的layer并不是CAGradientLayer,而是其他类型的layer,如UILabel的layer其实是_UILabelLayer,我们在设置layer属性时不能确保这个layer就是CAGradientLayer,需要加个判断:if ([self.layer isKindOfClass:[CAGradientLayer class]]) {

// do something

}

否则可能会出现崩溃。

对于UILabel这种貌似已经重写过+layerClass方法的view,我目前是直接在其分类中再次重写+layerClass方法//  UIView+Gradient.m

@implementation UILabel (Gradient)

+ (Class)layerClass {

return [CAGradientLayer class];

}

@end

将_UILabelLayer替换为CAGradientLayer除了colors和backgroundColor的优先级不同其他还有什么影响暂时不清楚。作者:水暮竹妖

链接:https://www.jianshu.com/p/e7c9e94e165b

border渐变 ios_iOS一个方法搞定view渐变色相关推荐

  1. 手机android id修改密码,手机锁屏密码忘了?一个方法搞定

    今天,我们就分几种情况来解决这个难题. 如何清除苹果手机的锁屏密码你有手机注册时的ID和密码: iTunes软件界面 那很好办,如上图,电脑上下载iTunes软件进行恢复出厂即可,这样就可以清除密码. ...

  2. 一个方法搞定安卓路由跳转

    前言 团队分模块开发在大项目上很常见,但项目组A想跳转到一个项目组B负责的Activity,此时若B未创建该Activity,则会影响项目进度,由此,安卓路由跳转出现了 原理 原理比较简单,大家都知道 ...

  3. 仅一个方法搞定Flutter Http Dio 请求

    首先 pubspec.yaml里面添加dio依赖模块 dio: ^3.0.10 由于 http请求常用的是GET和POST方法, 请求参数格式要么JSON,要么XML, 所以我在第一次封装dio ht ...

  4. 封装一个类搞定90%安卓客户端与服务器端交互

    本实例封装了一个处理安卓客户端与服务器端交互的几个方法,对于中文乱码问题本实例也找到了解决方案.本例可以处理的场景如下: 1.与服务器端交互json数据. 2.Get方式与服务器端交互数据. 3.Po ...

  5. labview 重新启动后上次_还在为labview保存参数而苦恼吗?看看一个vi搞定全部

    2020labview懒人参数存取-升级版 问题:项目编程过程中,参数存取是无论如何也没法避免的一个环节,因为程序设置完成后,下次再打开时如果要执行上次设置的参数,就需要加载上次保存的参数文件.那么以 ...

  6. 通用人工智能最新突破!一个Transformer搞定一切

    Datawhale干货 编辑:梦晨 鱼羊,来源:量子位 通用人工智能,还得看DeepMind. 这回,只一个模型,使用相同的权重,不仅把看家本领雅达利游戏玩得飞起. 和人类聊聊天.看图写话也不在话下. ...

  7. 计算机怎么用函数计算销售额排名,用Excel算TOP项销售额占比?一个公式搞定所有!...

    原标题:用Excel算TOP项销售额占比?一个公式搞定所有! 作者 | 拉登Dony 来源 | 拉小登(id:ladengchupin) 今日目标: 计算Top项销售占比 昨天遇到了一个提问,非常的头 ...

  8. 最低销售量计算机公式,用Excel算TOP项销售额占比?一个公式搞定所有!

    原标题:用Excel算TOP项销售额占比?一个公式搞定所有! 作者 | 拉登Dony 来源 | 拉小登(id:ladengchupin) 今日目标: 计算Top项销售占比 昨天遇到了一个提问,非常的头 ...

  9. 将你的前端应用打包成docker镜像并部署到服务器?仅需一个脚本搞定

    1.前言 前段时间,自己搞了个阿里云的服务器.想自己在上面折腾,但是不想因为自己瞎折腾而污染了现有的环境.毕竟,现在的阿里云已经没有免费的快照服务了.要想还原的话,最简单的办法就是重新装系统.而一旦重 ...

最新文章

  1. matlab中隐含层缺省函数,Matlab双隐层的BP神经网络该如何创建
  2. 如何通过一个类名找到它属于哪个jar包?
  3. configure脚本分析
  4. Java使用JDBC连接随意类型数据库(mysql oracle。。)
  5. int型 判断奇偶_XSS(Reflected) 反射型跨站攻击
  6. 7教程统计意义_学渣的医学统计学自救笔记(一)
  7. 利用多线程提高程序性能(for Android)
  8. Linux开发_最全在Ubnutu环境下为你的程序设置快捷启动项和启动时管理员权限
  9. Android 一步步教你从ActionBar迁移到ToolBar
  10. 获取Nist的美国官方标准时间的解决办法
  11. 绿幕抠图-为你的想象插上翅膀
  12. android 手势密码 开发,Android自定义控件实现手势密码
  13. Windows bat批处理常用指令,常用指令及语法总结
  14. JavaScript设计模式:四、发布订阅模式
  15. Autojs微信自动操作免root脚本源码
  16. xp电脑系统时间同步服务器不可用,winxp系统不自动设置无法同步internet时间的解决方案...
  17. 六个好用的在线代码编辑器,你选哪个?
  18. 输入经纬度在地图中标注位置(百度地图)
  19. 怎么样用香港主机搭建游戏网站
  20. 使用Python绘制移动均线

热门文章

  1. 程序员的奋斗史(十八)——人活着,只是一种态度
  2. (二):多模态机器学习:综述与分类
  3. AForge.net 使用之录像拍照功能实现
  4. 大数据项目之电商数仓、业务数据介绍、电商系统表结构
  5. eos采用的共识机制是_EOS共识机制详解
  6. c语言接受mysql中文,C语言连接MySQL中文问题
  7. Scratch学习有什么优点
  8. Enumeration
  9. 全波形反演的深度学习方法: 第 4 章 基于正演的 FWI
  10. “二舅”火了,自媒体短视频“爆火”的基本要素,你知道吗?