效果图1.1
效果图1.2
创建继承于UIView的视图 .h文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// 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, strong) UIColor * _Nonnull titleTextColor; // delegate @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; |
.m 实现部分
定义用到的宏
1 2 3 4 |
#define ScreenWidth [UIScreen mainScreen].bounds.size.width #define ScreenHeight [UIScreen mainScreen].bounds.size.height #define Length 5 #define Length2 15 |
1 2 3 4 5 |
@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 |
自定义初始化方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
- (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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#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, startY); CGContextAddLineToPoint(context, startX - Length, startY - Length); CGContextAddLineToPoint(context, startX + Length, startY - Length); } break; } case XTTypeOfLeftUp: case XTTypeOfLeftCenter: case XTTypeOfLeftDown: { { 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 XTTypeOfRightUp: case XTTypeOfRightCenter: case XTTypeOfRightDown: { { 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; } } CGContextClosePath(context); [self.backGoundView.backgroundColor setFill]; [self.backgroundColor setStroke]; |