1.h

#import <UIKit/UIKit.h>

@interface UIView (Gradient)

/* The array of CGColorRef objects defining the color of each gradient

* stop. Defaults to nil. Animatable. */

@property(nullable, copy) NSArray *colors;

/* An optional array of NSNumber objects defining the location of each

* gradient stop as a value in the range [0,1]. The values must be

* monotonically increasing. If a nil array is given, the stops are

* assumed to spread uniformly across the [0,1] range. When rendered,

* the colors are mapped to the output colorspace before being

* interpolated. Defaults to nil. Animatable. */

@property(nullable, copy) NSArray<NSNumber *> *locations;

/* The start and end points of the gradient when drawn into the layer's

* coordinate space. The start point corresponds to the first gradient

* stop, the end point to the last gradient stop. Both points are

* defined in a unit coordinate space that is then mapped to the

* layer's bounds rectangle when drawn. (I.e. [0,0] is the bottom-left

* corner of the layer, [1,1] is the top-right corner.) The default values

* are [.5,0] and [.5,1] respectively. Both are animatable. */

@property CGPoint startPoint;

@property CGPoint endPoint;

+ (UIView *_Nullable)gradientViewWithColors:(NSArray<UIColor *> *_Nullable)colors locations:(NSArray<NSNumber *> *_Nullable)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint;

- (void)setGradientBackgroundWithColors:(NSArray<UIColor *> *_Nullable)colors locations:(NSArray<NSNumber *> *_Nullable)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint;

@end

2.m

#import "UIView+Gradient.h"

#import <objc/runtime.h>

@implementation UIView (Gradient)

+ (Class)layerClass {

return [CAGradientLayer class];

}

+ (UIView *)gradientViewWithColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)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<UIColor *> *)colors locations:(NSArray<NSNumber *> *)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;

}

#pragma mark- Getter&Setter

- (NSArray *)colors {

return objc_getAssociatedObject(self, _cmd);

}

- (void)setColors:(NSArray *)colors {

objc_setAssociatedObject(self, @selector(colors), colors, OBJC_ASSOCIATION_COPY_NONATOMIC);

if ([self.layer isKindOfClass:[CAGradientLayer class]]) {

[((CAGradientLayer *)self.layer) setColors:self.colors];

}

}

- (NSArray<NSNumber *> *)locations {

return objc_getAssociatedObject(self, _cmd);

}

- (void)setLocations:(NSArray<NSNumber *> *)locations {

objc_setAssociatedObject(self, @selector(locations), locations, OBJC_ASSOCIATION_COPY_NONATOMIC);

if ([self.layer isKindOfClass:[CAGradientLayer class]]) {

[((CAGradientLayer *)self.layer) setLocations:self.locations];

}

}

- (CGPoint)startPoint {

return [objc_getAssociatedObject(self, _cmd) CGPointValue];

}

- (void)setStartPoint:(CGPoint)startPoint {

objc_setAssociatedObject(self, @selector(startPoint), [NSValue valueWithCGPoint:startPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);

if ([self.layer isKindOfClass:[CAGradientLayer class]]) {

[((CAGradientLayer *)self.layer) setStartPoint:self.startPoint];

}

}

- (CGPoint)endPoint {

return [objc_getAssociatedObject(self, _cmd) CGPointValue];

}

- (void)setEndPoint:(CGPoint)endPoint {

objc_setAssociatedObject(self, @selector(endPoint), [NSValue valueWithCGPoint:endPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);

if ([self.layer isKindOfClass:[CAGradientLayer class]]) {

[((CAGradientLayer *)self.layer) setEndPoint:self.endPoint];

}

}

3.调用

- (void)gradientTest {

self.view.backgroundColor = [UIColor whiteColor];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 200, 30)];

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 120, 200, 30)];

UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 160, 200, 30)];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 200, 200, 30)];

[self.view addSubview:label];

[self.view addSubview:btn];

[self.view addSubview:tempView];

[self.view addSubview:imageView];

label.backgroundColor = [UIColor clearColor];

btn.backgroundColor = [UIColor blueColor];

tempView.backgroundColor = [UIColor blueColor];

imageView.backgroundColor = [UIColor blueColor];

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

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

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

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

label.text = @"Text";

label.textAlignment = NSTextAlignmentCenter;

[btn setTitle:@"Button" forState:UIControlStateNormal];

}

@end

@implementation UILabel (Gradient)

+ (Class)layerClass {

return [CAGradientLayer class];

}

@end

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

在网上找的代码,如有侵权,请联系本人,谢谢

渐变UI的更多相关文章

  1. photoshop基础教程视频-贺叶铭-传智播客-笔记

    界面构成 1.菜单栏 2.工具箱 3.工具属性栏 4.悬浮面板 5.画布 ctrl+n 新建对话框 (新建画布) 画布200*200大小,是指以毫米为单位,当不说单位,默认是毫米. 打开对话框:ctr ...

  2. UI设计趋势:渐变设计风格重新回归主流

    在扁平化设计刚刚兴起之时,渐变是设计师们避之不及的设计手法,然而今天它已经正式回归.几乎是在一夜之间,无数的网站开始使用渐变色. 从背景到图片上的色彩叠加,包括UI元素上所遮盖的色彩,所有的这一切都表 ...

  3. jQuery ui背景色动态渐变导航菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. UI 设计中的渐变

    简评: 渐变是通过两种或多种不同的色彩来绘制一个元素,同时在颜色的交界处进行衰减变化的一种设计.从拟物到扁平再到渐变,人们慢慢发现它能创造出从未有过的一种色彩感觉 -- 独特.现代和清爽.(本文译者@ ...

  5. (UWP)通过编写算法实现在地图中的渐变路径

    目前的一个App中需要实现这个需求,但是在UWP自带的Bing Map中,绘制的MapPolyline的StrokeColor的类型是Windows.UI.Color,也就是说一条MapPolylin ...

  6. iOS 2D绘图 (Quartz2D)之阴影和渐变(shadow,Gradient)

    原博地址:http://blog.csdn.net/hello_hwc/article/details/49507881 Shadow Shadow(阴影) 的目的是为了使UI更有立体感,如图 sha ...

  7. iOS开发UI篇—CAlayer层的属性

    iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...

  8. 浅谈UI设计中妙用无穷的深色系背景

    英文:medium 译者:优设网 - 陈子木 链接:http://www.uisdc.com/ui-benefits-of-dark-background# --------------------- ...

  9. css3实现颜色渐变以及兼容性处理

    有时我们会看到网站上的一些图片是渐变色的,这些图片有的是ui设计出来的,有的则是直接通过css3制作出来的.下面就讲一下css3实现渐变色的方法,以及在各个浏览器上的兼容性. CSS3 Gradien ...

随机推荐

  1. numpy交换列

    x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(x) x = x[:, [1, 0, 2]] print(x) 输出 [[1 2 3] [4 ...

  2. [Freemarker]自定义时间戳函数

    使用freemarker的web项目经常需要用在Url后面加上时间戳来保证资源不被缓存,我们可以自定义方法实现时间戳. 先看freemarker配置信息: <bean id="free ...

  3. Java数组之二维数组

    Java中除了一维数组外,还有二维数组,三维数组等多维数组.本文以介绍二维数组来了解多维数组. 1.二维数组的基础 二维数组的定义:二维数组就是数组的数组,数组里的元素也是数组. 二维数组表示行列二维 ...

  4. LeetCode-7-反转整数-c# 版本

    c# 版本 // 给定一个 32 位有符号整数,将整数中的数字进行反转. public class Solution { public int Reverse(int x) { / // 边界判断 / ...

  5. 让我怀疑人生的bug集合

    bug1:一个人人都知道全局变量易污染,但是我就是污染不了的问题 解决:刚开始动用了session来存这个值,后来觉得太小题大做了,最后使用了闭包来解决,第一个function结束后开启第二个,起初没 ...

  6. php普通传值和引用传值 (相当通俗易懂的一篇讲解)

    首先,要理解变量名存储在内存栈中,它是指向堆中具体内存的地址,通过变量名查找堆中的内存; 普通传值,传值以后,是不同的地址名称,指向不同的内存实体; 引用传值,传引用后,是不同的地址名称,但都指向同一 ...

  7. 北京大学Cousera学习笔记--8-计算导论与C语言基础--C的运算部分

    赋值运算符 1.两边类型不同:赋值时要进行类型转换,右边要转换到左边 2.长数赋值短数 最后的部分截断赋值给短数 3.短数赋给长数 数不变 4.符号位赋值 --计算机不区分符号位数字位,直接赋值 表达 ...

  8. Python学习笔记(Ⅰ)——Python程序结构与基础语法

    作为微软的粉丝,最后终于向Python低头了,拖了两三个月终于下定决心学习Python了.不过由于之前受到C/C#等语言影响的思维定式,前期有些东西理解起来还是很费了些功夫的. 零.先抄书: 1.Py ...

  9. 微信中H5网页如何唤醒打开外部浏览器打开指定链接

    最近遇到一个需求.朋友找我制作一个在微信中的聊天框,或者公众号菜单发布一条链接或者二维码.跳出微信打开一个指定的我们自己的页面, 拿到这个需求后我们团队分开去找资料研究方案,通过微信的开发文档.腾讯的 ...

  10. Linux交换Esc和Caps

    使用过 .xmodmap,重启后就失效,添加到rc.local也不管用,后来通过在xorg里配置成功. 更改xorg里的键盘配置,增加Option "XkbOptions" &qu ...