美文网首页
带箭头的UIView

带箭头的UIView

作者: 人生半阙将进酒 | 来源:发表于2017-08-24 08:49 被阅读0次

      在网上查资料的时候,看到带箭头的UIView的箭头都是直的,显得很没有平滑感,恰好当时正在写高德地图,高德地图中有一个气泡就是画的,正好拿来用一用!本来参考高德地图的气泡写过一个,不小心将代码遗失去,后来参考:http://www.jianshu.com/p/0c609bf5cb6f

    下面是经过改变路径后的代码:

    - (void)drawRect:(CGRect)rect {

            //默认圆角角度

             float    r =4;

           //居中偏移量(箭头高度)

           float      offset =5;

           //设置箭头位置

           float       positionNum =20;

           //设置画线长宽

            float        w =self.frame.size.width;

            float         h =self.frame.size.height;

            //获取文本

            CGContextRefcontext =UIGraphicsGetCurrentContext();

             //设置边线宽度

             CGContextSetLineWidth(context,0.2);

             //边框颜色

             CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor); 

             //矩形填充颜色

              CGContextSetFillColorWithColor(context, [UIColor     blueColor].CGColor);

              CGContextMoveToPoint(context,0, h);

             CGContextAddLineToPoint(context, w, h);//向右划线

             CGContextAddLineToPoint(context, w, offset);//向右上角划线

             CGContextAddLineToPoint(context, positionNum +20, offset);//向左划线

             CGContextAddArcToPoint(context, positionNum +10,0, positionNum, offset, r);//向             左上划曲线

             CGContextAddArcToPoint(context, positionNum, offset,0, offset, r);//向左下划曲线

             CGContextAddLineToPoint(context,0, offset);//向左顶点划                                 

             CGContextDrawPath(context,kCGPathFillStroke);//根据坐标绘制路径

            /**父类调用放在画完边线后.不然设置的文字会被覆盖*/

             [super   drawRect:rect];

     }

    相关文章

      网友评论

          本文标题:带箭头的UIView

          本文链接:https://www.haomeiwen.com/subject/afesdxtx.html