ios 画带有箭头的线_基于IOS实现带箭头的view

本文介绍如何在iOS中创建一个自定义View,该View能够从不同方向显示带有箭头的效果,类似微信的功能。通过重写drawRect方法绘制箭头,并提供多种展示类型。同时,该View还包含一个内部的UITableView,可以填充数据并响应点击事件。
摘要由CSDN通过智能技术生成

我使用DrawRect进行的View的拉伸(是这样描述的吧??), 效果图也实现了类似于微信的View效果, 你可以看一看.

创建继承于UIView的视图 .h文件

// backGoundView

@property (nonatomic, strong) UIView * _Nonnull backGoundView;

// titles

@property (nonatomic, strong) NSArray * _Nonnull dataArray;

// images

@property (nonatomic, strong) NSArray * _Nonnull images;

// height

@property (nonatomic, assign) CGFloat row_height;

// font

@property (nonatomic, assign) CGFloat fontSize;

// textColor

@property (nonatomic, assign) UIColor * _Nonnull titleTextColor;

// delegate

@property (nonatomic, assign) id _Nonnull delegate;

// 初始化方法

- (instancetype _Nonnull)initWithOrigin:(CGPoint) origin

Width:(CGFloat) width

Height:(CGFloat) height

Type:(XTDirectionType)type

Color:( UIColor * _Nonnull ) color;

- (void)popView;

- (void)dismiss;

##.m 实现部分

定义用到的宏

#define ScreenWidth [UIScreen mainScreen].bounds.size.width

#define ScreenHeight [UIScreen mainScreen].bounds.size.height

#define Length 5

#define Length2 15

@property (nonatomic, assign) CGPoint origin; // 箭头位置

@property (nonatomic, assign) CGFloat height; // 视图的高度

@property (nonatomic, assign) CGFloat width; // 视图的宽度

@property (nonatomic, assign) XTDirectionType type; // 箭头位置类型

@property (nonatomic, strong) UITableView *tableView; // 填充的tableview

自定义初始化方法

- (instancetype)initWithOrigin:(CGPoint)origin Width:(CGFloat)width Height:(CGFloat)height Type:(XTDirectionType)type Color:(UIColor *)color

{

self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];

if (self) {

self.backgroundColor = [UIColor clearColor];

self.origin = origin;

self.width = width;

self.height = height;

self.type = type;

self.backGoundView = [[UIView alloc] initWithFrame:CGRectMake(origin.x, origin.y, width, height)];

self.backGoundView.backgroundColor = color;

[self addSubview:self.backGoundView];

[self.backGoundView addSubview:self.tableView];

}

return self;

}

drawRect

#pragma mark - drawRect

- (void)drawRect:(CGRect)rect {

// Drawing code

CGContextRef context = UIGraphicsGetCurrentContext();

switch (self.type) {

case XTTypeOfUpLeft:

case XTTypeOfUpCenter:

case XTTypeOfUpRight:{

{

CGFloat startX = self.origin.x;

CGFloat startY = self.origin.y;

CGContextMoveToPoint(context, startX, startY);

CGContextAddLineToPoint(context, startX + Length, startY + Length);

CGContextAddLineToPoint(context, startX - Length, startY + Length);

}

break;

}

case XTTypeOfDownLeft:

case XTTypeOfDownCenter:

case XTTypeOfDownRight: {

{

CGFloat startX = self.origin.x;

CGFloat startY = self.origin.y;

CGContextMoveToPoint(context, startX, st

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值