创建一个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的更多相关文章

  1. ios中用drawRect方法绘图的时候设置颜色

    设置画笔颜色可以直接 [[UIColor grayColor] set];就可以设置颜色.

  2. ios假设写一个提示带动画的View,能够来引导用户行为

    先上图: 这个UIView能够这样写: -(id)initWithFrame:(CGRect)frame backImage:(UIImage*)image msgStr:(NSString*)txt ...

  3. 自定义View 一 (继承VIew重写onDraw方法)

    项目:具有圆形效果的自定义View 一.继承View并重写onDraw方法 public class CircleView extends View{ private static final int ...

  4. iOS 新浪微博-2.0 搜索框/标题带箭头/下拉菜单

    不管是搜索框还是下拉菜单,我们都需要对背景的图片进行拉伸.定义一个Category分类对图片进行操作. UIImage+Effect.h #import <UIKit/UIKit.h> @ ...

  5. IOS开发中重写init方法使用需谨慎

    IOS开发中重写init方法使用需谨慎 今天在写一个小软件的时候出现一点问题,这个软件的功能是搜索全国学校,首页就是搜索输入框,在框中输入完要查询的学校所在省份,点击buttom后就会跳转到对应的视图 ...

  6. Asp.Net实现JS前台带箭头的流程图方法总结!(个人笔记,信息不全)

    Asp.Net实现JS前台带箭头的流程图方法总结!(持续更新中) 一.返回前台json格式 json5 = "[{\"Id\":2259,\"Name\&quo ...

  7. ios中怎么样设置drawRect方法中绘图的位置

    其中drawRect方法中的参数rect就是用来设置位置的,

  8. iOS的layoutSubviews和drawRect方法何时调用

    layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews.2.addSubview会触发layoutSubviews.3.设置view的Frame会触 ...

  9. iOS开发UI篇-懒加载、重写setter方法赋值

    一.懒加载 1.懒加载定义 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再 ...

随机推荐

  1. EasyDSS流媒体服务器灵活地帮助用户实现摄像机RTSP转RTMP直播功能

    简要描述 今天突然接到国内某上市公司同事打来的技术咨询电话,经过简单的沟通,大概所描述的需求是: 1.目前现场有非常多的摄像机资源需要接入: 2.需要将摄像机的RTSP流转成RTMP流接入到微信小程序 ...

  2. ArcGIS API for javascript Bookmarks(书签)示例2

    1.运行效果图 说明:这篇博文介绍的书签位于地图之上 有关博文中引用的API文件 怎么iis上部署,请参考我前面的博文 2.HTML代码 <!DOCTYPE html> <html ...

  3. vue表单输入的绑定

    vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...

  4. oracle chain

    [oracle@tyger dbs]$ sqlplus / as sysdba SQL*Plus: Release 10.2.0.1.0 - Production on Tue May 6 13:02 ...

  5. [转载]Hibernate如何提升数据库查询的性能

    目录(?)[-] 数据库查询性能的提升也是涉及到开发中的各个阶段在开发中选用正确的查询方法无疑是最基础也最简单的 SQL语句的优化 使用正确的查询方法 使用正确的抓取策略 Hibernate的性能优化 ...

  6. web audio living

    总结网页音频直播的方案和遇到的问题. 代码:(github,待整理) 结果: 使用opus音频编码,web audio api 播放,可以达到100ms以内延时,高质量,低流量的音频直播. 背景: V ...

  7. C#实例,熟练使用泛型数组等,课程选择小软件

    CourseItem.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  8. 牛客小白月赛1 F 三视图 【循环】

    题目链接 https://www.nowcoder.com/acm/contest/85/F 思路 记录每一个面 上的点 是否有方块 然后 根据它的输出顺序 遍历访问 如果有 输出 'X' 否则 输出 ...

  9. vim 的复制粘贴命令,以及使用寄存器来存放要复制的内容;

    一,y(yanks)复制,p(paste)粘贴: yy 复制当前行,2yy,复制2行: Y  复制整行:Y=yy; p 粘贴到光标后: P 粘贴到光标前: 注意vim会知道你复制内容的是整行还是一个矩 ...

  10. C# 中获取CPU序列号/网卡mac地址

    1.cpu序列号2.mac序列号3.硬盘id在给软件加序列号时这三个应该是最有用的,可以实现序列号和机器绑定,对保护软件很有好处.哈哈.   using System; using System.Ma ...