iOS(CoreGraphics)画带箭头的框
2016-02-19 17:47
483 查看
自己画带箭头的框,需要导入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画图)
新建一个集成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画图)
相关文章推荐
- iOS 原生二维码扫描(可限制扫描区域)
- ios布局-与xib结合
- ios自定义归档解档
- JSPatch-动态更新IOS APP
- iOS比较好的几个博客网址
- <转>iOS深拷贝与浅拷贝 附NSSet讲解
- iOS 知识点
- iOS 52个技巧学习心得笔记 第二章 对象 , 消息, 运行期
- ios 原生二维码的扫描和生成
- tableview取消CELL的多余行数
- iOS开发中那些高效常用的宏
- ios开发- 将字符串切割成数组 componentSeparatedByString
- 仿ios的对话框
- iOS设计模式-工厂方法
- 解决Xcode7.1上传成功但iTunesconnect一直不能构建版本的问题
- iOS9适配
- iOS开发中的几种设计模式介绍
- iOS中延时执行方法的比较和汇总
- IOS第四天_面向对象2
- ios开发之itunes打包流程