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方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再 ...
随机推荐
- Fiddler 详尽教程与抓取移动端数据包
转载自:http://blog.csdn.net/qq_21445563/article/details/51017605 阅读目录 1. Fiddler 抓包简介 1). 字段说明 2). Stat ...
- 【BZOJ3563/3569】DZY Loves Chinese II 线性基神题
[BZOJ3563/3569]DZY Loves Chinese II Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅Dzy以降. 纷Dzy既有此内美兮,又重之以 ...
- nginx学习之web服务器(四)
1. 定义一个虚拟服务器 http { server { # Server configuration } } 可以在http {}块里面添加多个server {}块,每一个server {}块代表一 ...
- php解析xml文件为数组
$xml = simplexml_load_file($fullfilename); $arr = json_decode(json_encode($xml),true); echo "&l ...
- python基础-第六篇-6.4模块混战
我们之前接触多的编程方式就是函数式编程,而且喜欢就一个文件里写完所有的程序代码,这样做在前期感觉还不错,不过一旦你的程序变复杂,在易读性和排错方面就感觉好吃力,功能界限不明显,那今天我们就来讲讲怎么用 ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)
在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...
- 阿里云centos7搭建php+nginx环境
阿里云Centos搭建lnmp(php7.1+nginx+mysql5.7) https://jingyan.baidu.com/article/215817f7a10bfb1eda14238b.ht ...
- 如何在JSTL中获取数组或者list对象的索引值(index)
<c:forEach items="${productList}" var="products" varStatus="status" ...
- HDU - 5695 Gym Class 【拓扑排序】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...
- 数组元素的删除 【vector】
7-5 数组元素的删除(5 分) 完成数组元素的移动功能:假设数组有n个元素,输入一个数x,把数组的第x个位置的元素删除了,后面的元素依次前进一个位置. 重复若干次这样的删除,得到最后的结果. 输入格 ...