在iOS中系统框架提供了好多方法来供我们绘图,今天我们就研究渐变色.

1   用CAGradientLayer来实现

我们可以用CAGradientLayer来实现给试图添加渐变色.关于CAGradientLayer我们点击进去会发现主要有以下几个属性@interface CAGradientLayer : CALayer

@property(nullable, copy) NSArray *colors;

@property(nullable, copy) NSArray *locations;

@property CGPoint startPoint;

@property CGPoint endPoint;

@property(copy) NSString *type;

@end

解释下各个属性

colors 渐变的颜色

locations 渐变颜色的分割点

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

CAGradientLayer 继承于CALayer ,所以CAlayer得各个方法我们也可以使用.好了知识复习完毕 我们开始操作,先上代码#pragma mark  渐变色

-(void)gradineViewOne

{

CAGradientLayer* gradinentlayer=[CAGradientLayer layer];

gradinentlayer.colors=@[(__bridge id)[UIColor greenColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor,(__bridge id)[UIColor redColor].CGColor];

//分割点  设置 风电设置不同渐变的效果也不相同

gradinentlayer.locations=@[@0.2,@0.4,@1.0];

gradinentlayer.startPoint=CGPointMake(0, 0);

gradinentlayer.endPoint=CGPointMake(1.0, 0);

gradinentlayer.frame=CGRectMake(20, 100, 300, 400);

[self.view.layer  addSublayer:gradinentlayer];

}

效果图

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

很显然知识这样并不能满足我们实际生活中所需要面对的需求.那么还有其他方法吗?答案是肯定的.

下面我们用另外的方法实现

2  Core Graphics相关方法实现渐变

OS Core Graphics中有两个方法用于绘制渐变颜色,CGContextDrawLinearGradient可以用于生成线性渐变,CGContextDrawRadialGradient用于生成圆半径方向颜色渐变。

我们先完成线性渐变的处理  上代码

线性渐变//线性渐变

-(void)makeLineGradient

{

//创建CGContextRef

UIGraphicsBeginImageContext(self.view.bounds.size);

//创建绘图上下文

CGContextRef gcontext = UIGraphicsGetCurrentContext();

//创建CGMutablePathRef

CGMutablePathRef path = CGPathCreateMutable();

//绘制Path

CGRect rect = CGRectMake(20, 100, 300, 300);

CGPathMoveToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));

CGPathAddLineToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMaxY(rect));

CGPathAddLineToPoint(path, NULL, CGRectGetWidth(rect), CGRectGetMaxY(rect));

CGPathCloseSubpath(path);

//渐变绘制

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGFloat locations[] = { 0.0, 1.0 };

CGColorRef startColor =[UIColor greenColor].CGColor;

CGColorRef endColor =[UIColor redColor].CGColor;

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(gcontext);

CGContextAddPath(gcontext, path);

CGContextClip(gcontext);

CGContextDrawLinearGradient(gcontext, gradient, startPoint, endPoint, 0);

CGContextRestoreGState(gcontext);

CGGradientRelease(gradient);

CGColorSpaceRelease(colorSpace);

//注意释放CGMutablePathRef

CGPathRelease(path);

//从Context中获取图像,并显示在界面上

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

[self.view addSubview:imgView];

}

上效果图

半径渐变//半径渐变

-(void)makeradisGradient

{

//创建CGContextRef

UIGraphicsBeginImageContext(self.view.bounds.size);

//创建绘图上下文

CGContextRef gcontext = UIGraphicsGetCurrentContext();

//创建CGMutablePathRef

CGMutablePathRef path = CGPathCreateMutable();

//绘制Path

CGRect rect = CGRectMake(50, 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);

//渐变绘制

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGFloat locations[] = { 0.0, 1.0 };

CGColorRef startColor =[UIColor greenColor].CGColor;

CGColorRef endColor =[UIColor blackColor].CGColor;

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(gcontext);

CGContextAddPath(gcontext, path);

CGContextEOClip(gcontext);

CGContextDrawRadialGradient(gcontext, gradient, center, 0, center, radius, 0);

CGContextRestoreGState(gcontext);

CGGradientRelease(gradient);

CGColorSpaceRelease(colorSpace);

//注意释放CGMutablePathRef

CGPathRelease(path);

//从Context中获取图像,并显示在界面上

UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

[self.view addSubview:imgView];

}

效果图

使用 CAShapeLayer 来实现

原理就是利用CALayer的mask属性

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

这样做的前提是先有一个渐变色的图片 然后利用CAShapeLayer 来自定义形状mask遮住不想要的部分,显示出我们想要的样式.

上代码//使用cashapelayer 实现渐变

-(void)gradineViewTwo

{

UIImageView *gCircle=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"circleBackground"]];

[self.view addSubview:gCircle];

gCircle.layer.masksToBounds = YES;

gCircle.alpha = 1.0;

gCircle.frame=CGRectMake(0, 0, 200, 200);

gCircle.center = CGPointMake(CGRectGetWidth(self.view.bounds) / 2.0, CGRectGetHeight(self.view.bounds) / 2.0);

CGFloat lineWidth = 5;

//生成shapelayer

CAShapeLayer *wavelinelayer = [CAShapeLayer layer];

wavelinelayer.lineCap = kCALineCapButt;

wavelinelayer.lineJoin = kCALineJoinRound;

wavelinelayer.strokeColor = [UIColor redColor].CGColor;

wavelinelayer.fillColor = [[UIColor clearColor] CGColor];

wavelinelayer.lineWidth = lineWidth;

wavelinelayer.backgroundColor = [UIColor clearColor].CGColor;

//生成path 自定义形状

CGPoint center =CGPointMake(100, 100);

//调整半径和中心点的位置 遮住的部分不一样

CGFloat radius=95;

UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:2*M_PI clockwise:NO];

wavelinelayer.path=circlePath.CGPath;

//CALayer的mask属性可以作为遮罩让layer显示mask遮住(非透明)部分

gCircle.layer.mask=wavelinelayer;

}

上原来的渐变图 和效果图

效果图

总结 实现渐变色其实还是主要是对Core Graphics 这个绘图框架的底层运用,涉及到CAShapeLayer等相关知识点.

border渐变 ios_iOS给视图添加渐变色相关推荐

  1. border渐变 ios_iOS实现颜色渐变

    我们经常会在UIView添加渐变的背景色.虽然找一张渐变颜色的背景图很方便,但是图片是要占用资源的,同时文件也会变大.所以,我们完全可以使用代码来实现效果. 下面是使用代码来写渐变色的方法. 1.使用 ...

  2. border渐变 ios_iOS一个方法搞定view渐变色

    Demo 使用效果//包含头文件UIView+Gradient.h [self.label setGradientBackgroundWithColors:@[[UIColor redColor],[ ...

  3. border渐变 ios_IOS画渐变的三种方式

    1. CAGradientLayer实现渐变 CAGradientLayer是CALayer的一个特殊子类,用于生成颜色渐变的图层,使用较为方便,下面介绍下它的相关属性: colors    渐变的颜 ...

  4. 直播视频app源码文本添加渐变色

    直播视频app源码,文本添加渐变色相关的代码 1.使用Text组件显示一段文字 Center(heightFactor: 1.0,child: Container(child: Text(" ...

  5. iOS UIView添加渐变色

    #import <UIKit/UIKit.h>@interface UIView (TransitionColor)//添加渐变色 - (void)addTransitionColor:( ...

  6. 视图添加字段_使用ExploreByTouchHelper辅助类为自定义视图添加虚拟视图

    在安卓开发过程中,为了视觉和功能的需要开发者经常会使用自定义视图 大多数的自定义视图是组合现有的控件来完成特定的功能 但是,有一种自定义视图是通过画笔在画布上画出自定义的子视图的,例如日期控件,颜色选 ...

  7. css border渐变_css边框渐变

    在实际开发中,我们经常遇见边框需要背景渐变的实现要求,那么如何去实现呢,今天给大家分享依稀几种情况 1.直角的背景渐变 border渐变进入平台 注意问题:border-image的使用是不能实现圆角 ...

  8. ECharts(三):柱状图柱体颜色渐变(每个柱体不同渐变色)

    前提: 会简单的绘制柱状图 主要内容: 渐变的主要使用在https://efe.baidu.com/blog/echarts-3.2.0/中有介绍到: itemStyle: {     normal: ...

  9. Icon Status CDS视图 添加信号灯状态 Fiori Element- Criticality

    通过CDS视图添加信号灯,在Fiori前端显示状态图标,效果如下 语义图标颜色 要实现图标,需要把目标字段映射到UI.Criticality 完整code: @AbapCatalog.sqlViewN ...

最新文章

  1. 2021 CSDN年度回忆录
  2. IOS开发系列--IOS程序开发概览
  3. Android 动画之TranslateAnimation应用详解
  4. 汇编语言 + Visual Studio 2019——Visual Studio 2019 中汇编语言环境解决方案
  5. 【编译原理】文法分类
  6. 哈希表创建哈希表(Hash Table,也叫散列表),是根据键(Key)而直接访问在内存存储位置的数据结构.typedef enum{ HASH_OK-icoding-数据结构-C
  7. java 泛型参数的类型_Java获得泛型参数类型
  8. web性能优化(理论)
  9. python元素定位id和name_python中通过selenium简单操作及元素定位知识点总结
  10. 聊城a货翡翠,大同a货翡翠
  11. python代码加密cython_python通过cython实现加密
  12. CentOS 下 Oracle 10g 安装 + 配置 全过程(图解)
  13. 串的复制——串传送指令MOVSB和方向标志位DF和CLD和REP
  14. win10 uwp 渲染原理 DirectComposition 渲染
  15. 操作系统实验 文件复制/复制文件
  16. 执行安装操作的时候,出现丢失MSVCR120.dll的解决方法
  17. 路由交换实验一——CISCO路由器的基本配置
  18. 在 CentOS 上构建无线网络测试环境
  19. clip studio paint插件开发之介绍
  20. Electron加密打包文件

热门文章

  1. https 双向认证开发实践
  2. freeswitch智能语音开发之ASR
  3. 笔画输入法教程――如何学习笔画输入法
  4. pc端和移动端集成第三方快捷登录 --- 微博为例
  5. IP地址冲突怎么办? 如何解决局域网IP地址冲突?
  6. Matlab图像分割---使用dice相似系数方法进行图像分割精度验证
  7. 软件企业认定的标准要求
  8. 基于语音控制的智能家居实现
  9. B. Deadly Laser
  10. C语言LMS双麦克风消噪算法,芯片内部的噪声抑制算法,语音芯片来说也是一样(双麦克风降噪理念)...