ios纯色箭头与渐变色箭头的实现

UIView默认是矩形  我们可以通过重写drawRect来改变形状。

图1_demo

1、实现黑色箭头我们需要自定义一个view,在.m文件中实现如下代码:

- (void)drawRect:(CGRect)rect {

[super drawRect:rect];

CGContextRef context =UIGraphicsGetCurrentContext();

CGContextSaveGState(context);

//颜色

CGContextSetFillColor(context,CGColorGetComponents(RGBA(34,36,45,1).CGColor));

CGMutablePathRefpath1 =CGPathCreateMutable();

constCGAffineTransformtran[] = {

CGAffineTransformIdentity

};

//绘制的线条(width和height代表当前view的宽度和高度)

CGPointlines[] = {

CGPointMake(0,0),

CGPointMake(width-height/2,0),

CGPointMake(width,height/2),

CGPointMake(width-height/2,height),

CGPointMake(0,height),

CGPointMake(0,0),

};

CGPathAddLines(path1, tran, lines,6);

CGContextAddPath(context, path1);

CGContextClosePath(context);

CGContextFillPath(context);

CGPathRelease(path1);

}

2、颜色渐变箭头也是要自定义view,不过具体实现方法不一样,实现代码如下:

- (void)drawRect:(CGRect)rect{

[superdrawRect:rect];

CGContextRefcontext =UIGraphicsGetCurrentContext();

//创建一个RGB的颜色空间

CGColorSpaceRefrgb =CGColorSpaceCreateDeviceRGB();

//定义渐变颜色数组

CGFloatcolors[] =

{

255.0/255.0,188.0/255.0,84.0/255.0,1.00,

255.0/255.0,108.0/255.0,70.0/255.0,1.00,

};

CGGradientRefgradient =CGGradientCreateWithColorComponents(rgb, colors,NULL,sizeof(colors)/(sizeof(colors[0])*4));

CGColorSpaceRelease(rgb);

CGContextSaveGState(context);

CGContextMoveToPoint(context,0,0);

//(width和height代表当前view的宽度和高度)

CGContextAddLineToPoint(context,width-height/2,0);

CGContextAddLineToPoint(context,width,height/2);

CGContextAddLineToPoint(context,width-height/2,height);

CGContextAddLineToPoint(context,0,height);

CGContextClip(context);

CGContextDrawLinearGradient(context, gradient,CGPointMake

(1,0) ,CGPointMake(width,width),

kCGGradientDrawsAfterEndLocation);

CGContextRestoreGState(context);

}

调用时直接像平常一样创建即可

图2_创建

注意:view初始化之后,一定要设置背景颜色透明,设置背景颜色覆盖绘制的路径。

原谅我实在不知道怎样插入代码块~😘

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • --绘图与滤镜全面解析 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益...
    韩七夏阅读 2,791评论 2 10
  • 前言:关于贝塞尔曲线与CAShapeLayer的学习 学习Demo演示: 贝塞尔曲线简单了解 使用UIBezier...
    麦穗0615阅读 17,917评论 18 149
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,141评论 5 13
  • Quartz2D以及drawRect的重绘机制字数1487 阅读21 评论1 喜欢1一、什么是Quartz2D Q...
    PurpleWind阅读 804评论 0 3
  • 春暖花开的季节……遇上春雨绵绵,人生就像坐火车,爱情走走停停,朋友去去留留,有人中途下车,有人半路上车,多少人与你...
    HP取舍阅读 599评论 0 0