iOS(CoreGraphics)画带箭头的框 - 日理万妓 2022-09-24 06:20 269阅读 0赞 自己画带箭头的框,需要导入CoreGraphics.framwork库 新建一个集成UIView 的类,对UIView中的drawRect:(CGRect)rect方法重写如下。 绘图操作 //绘制带箭头的矩形 \-(void)drawArrowRectangle:(CGRect) frame \{ // 获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸 CGContextRef ctx =UIGraphicsGetCurrentContext(); // 创建一个新的空图形路径。 CGContextBeginPath(ctx); //启始位置坐标x,y CGFloat origin\_x = frame.origin.x; CGFloat origin\_y = frame.origin.y; //第一条线的位置坐标 CGFloat line\_1\_x = frame.size.width; CGFloat line\_1\_y = origin\_y; //第二条线的位置坐标 CGFloat line\_2\_x = line\_1\_x; CGFloat line\_2\_y = frame.size.height; //第三条线的位置坐标 CGFloat line\_3\_x = origin\_x + 20; CGFloat line\_3\_y = line\_2\_y; //尖角的顶点位置坐标 CGFloat line\_4\_x = line\_3\_x - 5; CGFloat line\_4\_y = line\_2\_y + 10; //第五条线位置坐标 CGFloat line\_5\_x = line\_4\_x - 5; CGFloat line\_5\_y = line\_3\_y; //第六条线位置坐标 CGFloat line\_6\_x = origin\_x; CGFloat line\_6\_y = line\_2\_y; CGContextMoveToPoint(ctx, origin\_x, origin\_y); CGContextAddLineToPoint(ctx, line\_1\_x, line\_1\_y); CGContextAddLineToPoint(ctx, line\_2\_x, line\_2\_y); CGContextAddLineToPoint(ctx, line\_3\_x, line\_3\_y); CGContextAddLineToPoint(ctx, line\_4\_x, line\_4\_y); CGContextAddLineToPoint(ctx, line\_5\_x, line\_5\_y); CGContextAddLineToPoint(ctx, line\_6\_x, line\_6\_y); CGContextClosePath(ctx); UIColor \*costomColor = \[UIColorcolorWithWhite:0alpha:0.8\]; CGContextSetFillColorWithColor(ctx, costomColor.CGColor); CGContextFillPath(ctx); \} //重写绘图,调用刚才绘图的方法 \-(void)drawRect:(CGRect)rect \{ CGRect frame = rect; frame.size.height = frame.size.height -20; rect = frame; //绘制带箭头的框框 \[selfdrawArrowRectangle:rect\]; \} 效果如下图:(带箭头的框中的信息是后添加的,这里只是介绍了利用CoreGraphics画图) ![Center][] [Center]: /images/20220719/fa70df8fa4d9408d89d85d516ac08fdb.png
还没有评论,来说两句吧...