创建一个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. css 坑记

    1. div 内容超出 (做换行处理)   要注意 white-space属性的运用 设置 div width:100%;(或者固定值) 设置换行  word-break: break-all; 设置 ...

  2. MAC 脚本批量启动应用

    1.touch batchStart.sh 2. #!/bin/bash cd /xxx open 1.app open 2.app 3.chmod +x batchStart.sh 4.ok

  3. Android环境搭建 NDK+ADT(免cywgin)

    JDK下载: 1下载地址  http://www.oracle.com/technetwork/java/javase/downloads/index.html 2配置环境变量 JAVA_HOME:创 ...

  4. [Java面试一]Spring总结以及在面试中的一些问题.(转发:http://www.cnblogs.com/wang-meng/p/5701982.html)

    1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spri ...

  5. setTimeout解决循环值的几种方法

    for(var i=0;i<5;i++){ setTimeout(function(){ console.log(`错误 ${i}`); },0) } for(var i=0;i<5;i+ ...

  6. eclipse新建Maven项目

    1.在eclipse中安装maven插件 2.点击File->new->maven project,出现弹窗后点击next. 接着在弹窗Select an Archetype中,filte ...

  7. [原创]java WEB学习笔记27:深入理解面向接口编程

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  8. 日期类(C++实现)

    //-------------------------------------------------------------------------- /* **功能:实现日期的简单操作 ** ** ...

  9. hd acm2035

    求A^B的最后三位数表示的整数.说明:A^B的含义是“A的B次方” 思路:后三位只跟后三位相乘有关,所以可以每乘一次都对1000取余. 代码: #include <stdio.h>#inc ...

  10. Linux内核源码中的likely和unlikely释疑【转】

    本文转载自:https://my.oschina.net/armsky/blog/15320 ikely()与unlikely()在2.6内核中,随处可见,那为什么要用它们?它们之间有什么区别呢? 首 ...