iOS重写drawRect方法实现带箭头的View
创建一个UIView的子类,重写drawRect方法可以实现不规则形状的View,这里提供一个带箭头View的实现代码:
ArrowView.h
#import <UIKit/UIKit.h> @interface ArrowView : UIView @end
ArrowView.m
#import "ArrowView.h" @implementation ArrowView /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ - (instancetype)init{ self = [super init];
if (self) {
self.backgroundColor = [UIColor whiteColor];
}
return self; } - (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
}
return self; } - (void)drawRect:(CGRect)rect{
[super drawRect:rect]; NSLog(@"正在drawRect..."); //获取当前图形,视图推入堆栈的图形,相当于你所要绘制图形的图纸
CGContextRef ctx = UIGraphicsGetCurrentContext();
//
[[UIColor whiteColor] set];
//创建一个新的空图形路径
CGContextBeginPath(ctx); NSLog(@"开始绘制..."); //起始位置坐标
CGFloat origin_x = rect.origin.x;
CGFloat origin_y = ; //frame.origin.y + 10;
//第一条线的位置坐标
CGFloat line_1_x = rect.size.width - ;
CGFloat line_1_y = origin_y;
//第二条线的位置坐标
CGFloat line_2_x = line_1_x + ;
CGFloat line_2_y = rect.origin.y;
//第三条线的位置坐标
CGFloat line_3_x = line_2_x + ;
CGFloat line_3_y = line_1_y;
//第四条线的位置坐标
CGFloat line_4_x = rect.size.width;
CGFloat line_4_y = line_1_y;
//第五条线的位置坐标
CGFloat line_5_x = rect.size.width;
CGFloat line_5_y = rect.size.height;
//第六条线的位置坐标
CGFloat line_6_x = origin_x;
CGFloat line_6_y = rect.size.height; 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 *customColor = [UIColor colorWithWhite: alpha:0.8];
CGContextSetFillColorWithColor(ctx, customColor.CGColor);
CGContextFillPath(ctx);
} @end
然后在ViewController中调用,查看结果
ViewController.m
#import "ViewController.h"
#import "ArrowView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. NSLog(@"begin..."); ArrowView *view = [[ArrowView alloc] initWithFrame:CGRectMake(, , , )];
//[view setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:view]; NSLog(@"end...");
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
结果截图:

控制台打印结果:

控制台打印的线程ID是相同的,说明drawRect的方法是在主线程调用的。
iOS重写drawRect方法实现带箭头的View的更多相关文章
- ios中用drawRect方法绘图的时候设置颜色
设置画笔颜色可以直接 [[UIColor grayColor] set];就可以设置颜色.
- ios假设写一个提示带动画的View,能够来引导用户行为
先上图: 这个UIView能够这样写: -(id)initWithFrame:(CGRect)frame backImage:(UIImage*)image msgStr:(NSString*)txt ...
- 自定义View 一 (继承VIew重写onDraw方法)
项目:具有圆形效果的自定义View 一.继承View并重写onDraw方法 public class CircleView extends View{ private static final int ...
- iOS 新浪微博-2.0 搜索框/标题带箭头/下拉菜单
不管是搜索框还是下拉菜单,我们都需要对背景的图片进行拉伸.定义一个Category分类对图片进行操作. UIImage+Effect.h #import <UIKit/UIKit.h> @ ...
- IOS开发中重写init方法使用需谨慎
IOS开发中重写init方法使用需谨慎 今天在写一个小软件的时候出现一点问题,这个软件的功能是搜索全国学校,首页就是搜索输入框,在框中输入完要查询的学校所在省份,点击buttom后就会跳转到对应的视图 ...
- Asp.Net实现JS前台带箭头的流程图方法总结!(个人笔记,信息不全)
Asp.Net实现JS前台带箭头的流程图方法总结!(持续更新中) 一.返回前台json格式 json5 = "[{\"Id\":2259,\"Name\&quo ...
- ios中怎么样设置drawRect方法中绘图的位置
其中drawRect方法中的参数rect就是用来设置位置的,
- iOS的layoutSubviews和drawRect方法何时调用
layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews.2.addSubview会触发layoutSubviews.3.设置view的Frame会触 ...
- iOS开发UI篇-懒加载、重写setter方法赋值
一.懒加载 1.懒加载定义 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再 ...
随机推荐
- phpcms的基础知识和配置
一.设置界面 1.站点设置:相当于服务器上的站点 (1)站点修改:“关键词”和“描述”的修改,便于网络优化和搜索引擎对本网站的搜索. (2)点击站点后边的修改,模板的修改,引用自己模板 2.基本设置: ...
- Educational Codeforces Round 1 (C) (atan2 + long double | 大数)
这题只能呵呵了. 东搞西搞,折腾快一天,最后用了一个800多行的代码AC了. 好好的题目你卡这种精度干啥. 还有要卡您就多卡点行不,为什么long double 又可以过... 废了N长时间写个了不管 ...
- <%%>与<scriptrunat=server>,<%=%>与<%#%>的区别(转)
这些东西都是asp.net前台页面与后台代码交互过程中经常使用的,它们之间有的非常相似,又有一些不同.对比学习下,看看他们之间的联系与区别. 首先看<%%>与<scriptrunat ...
- Maven项目启动报错
错误信息如下: 六月 , :: 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{S ...
- 变量动态选取资源ID
1.使用Resources 类的 getIdentifier方法 Resources res=getResources(); return res.getIdentifier(type ...
- CentOS6安装DaoCloud加速器
天朝的网,你又不是不懂.我最爱的红杏最近也用不了了.FUCK GFW. 在这,我们使用DaoCloud的加速器,打开网址 https://dashboard.daocloud.io/mirror 找到 ...
- Crontab使用详解
第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 下面是crontab的格式:分 时 日 月 星期 要运行的命令 这 ...
- iOS 动态修改导航栏颜色 UINavigationBar
示例 所谓动态修改 意思是 在当前页面滚动的过程中 亦或 是在 触发返回事件\进入一个新的页面 导航栏的动态变化 由于系统级别的navBar 高度集成 很多自己想实现的功能 很不好弄 如果是通过 ...
- HIVE 总结
http://blog.csdn.net/wisgood/article/details/17186181 常见错误 http://blog.csdn.net/sunnyyoona/article/d ...
- PHP 邮件发送类
mail.php <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件的邮件 * 需要的php扩展,sockets和Filei ...