本文示範如何透過 drawRect 繪製一個帶有方向箭頭的彈出式選單視圖,效果類似微信的加號選單,可支援上、下、左、右共十二種箭頭方位。

@property (nonatomic, strong) UIView * _Nonnull backGoundView; // 背景視圖 @property (nonatomic, strong) NSArray * _Nonnull dataArray; // 標題資料 @property (nonatomic, strong) NSArray * _Nonnull images; // 圖片資料 @property (nonatomic, assign) CGFloat row_height; // 列高 @property (nonatomic, assign) CGFloat fontSize; // 字級 @property (nonatomic, assign) UIColor * _Nonnull titleTextColor; // 文字顏色 @property (nonatomic, assign) id <selectIndexPathDelegate> _Nonnull delegate; // 代理 - (instancetype _Nonnull)initWithOrigin:(CGPoint)origin Width:(CGFloat)width Height:(CGFloat)height Type:(XTDirectionType)type Color:(UIColor * _Nonnull)color; - (void)popView; - (void)dismiss;
#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; // 內容表格
- (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 中繪製對應方向的三角形箭頭:
- (void)drawRect:(CGRect)rect {
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, startY);
CGContextAddLineToPoint(context, startX - Length, startY - Length);
CGContextAddLineToPoint(context, startX + Length, startY - Length);
break;
}
// Left / Right 系列同理,依方向調整座標增減
}
CGContextClosePath(context);
[self.backGoundView.backgroundColor setFill];
[self.backgroundColor setStroke];
CGContextDrawPath(context, kCGPathFillStroke);
}
popView 方法先將子檢視隱藏,再把自身加入 keyWindow,接著依方位計算起始 frame,並透過 startAnimateView 以動畫方式展開到目標尺寸,展開完成後再顯示子檢視:
- (void)startAnimateView_x:(CGFloat)x _y:(CGFloat)y origin_width:(CGFloat)width origin_height:(CGFloat)height {
[UIView animateWithDuration:0.25 animations:^{
self.backGoundView.frame = CGRectMake(x, y, width, height);
} completion:^(BOOL finished) {
for (UIView *view in [self.backGoundView subviews]) {
[view setHidden:NO];
}
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (![[touches anyObject].view isEqual:self.backGoundView]) {
[self dismiss];
}
}
內容以 UITableView 呈現,並透過自訂的 selectIndexPathDelegate 協定把點擊事件回傳給外部:
@protocol selectIndexPathDelegate <NSObject> - (void)selectIndexPathRow:(NSInteger)index; @end
CGPoint point = CGPointMake(_customBtn.center.x, _customBtn.frame.origin.y + 64); XTPopView *view1 = [[XTPopView alloc] initWithOrigin:point Width:130 Height:40 * 4 Type:XTTypeOfUpRight Color:[UIColor colorWithRed:0.2737 green:0.2737 blue:0.2737 alpha:1.0]]; view1.dataArray = @[@"發起群聊", @"添加朋友", @"掃一掃", @"收付款"]; view1.images = @[@"發起群聊", @"添加朋友", @"掃一掃", @"付款"]; view1.fontSize = 13; view1.row_height = 40; view1.titleTextColor = [UIColor whiteColor]; view1.delegate = self; [view1 popView];
外部只要實作代理方法 selectIndexPathRow: 即可處理各項目的點擊事件。
以上就是使用 drawRect 實作帶箭頭彈出式 View 的完整內容,透過自繪三角形與動畫展開,即可打造靈活的方向選單,希望對你的開發有所幫助。