下拉选择菜单封装

http://blog.csdn.net/u012701023/article/details/51778657


  1. //  
  2. //  OrderListDownMenu.h  
  3.   
  4. #import <UIKit/UIKit.h>  
  5.   
  6. @protocol OrderListDownMenuDelegate <NSObject>  
  7.   
  8. - (void)OrderListDownMenu:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;  
  9.   
  10. @end  
  11.   
  12. typedef void(^Dismiss)(void);  
  13.   
  14. @interface OrderListDownMenu : UIView<UITableViewDataSource, UITableViewDelegate>  
  15.   
  16. @property (nonatomicstrongUITableView *tableView;  
  17. @property (nonatomic, assign) id<OrderListDownMenuDelegate> delegate;  
  18. @property (nonatomicstrongNSArray *arrData;  
  19. @property (nonatomicstrongNSArray *arrImgName;  
  20. @property (nonatomiccopy) Dismiss dismiss;  
  21.   
  22. - (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight;  
  23.   
  24. - (void)dismissWithCompletion:(void (^)(OrderListDownMenu *object))completion;  
  25.   
  26. @end  
  1. #import "OrderListDownMenu.h"  
  2.   
  3. #define TopToView 63.0f  
  4. #define rightToView kScreenWidth - 15.0f  
  5. #define LeftToView kScreenWidth - 145.0 - 10.0f  
  6. #define CellLineEdgeInsets UIEdgeInsetsMake(0, -80, 0, 0)  
  7. #define kScreenWidth        [UIScreen mainScreen].bounds.size.width  
  8. #define kScreenHeight        [UIScreen mainScreen].bounds.size.height  
  9.   
  10. @interface OrderListDownMenu()  
  11.   
  12. @property (nonatomic, assign) CGPoint origin;  
  13. @property (nonatomic, assign) CGFloat rowHeight;  
  14.   
  15. @end  
  16.   
  17. @implementation OrderListDownMenu  
  18.   
  19. - (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight {  
  20.       
  21.     self = [super initWithFrame:CGRectMake(00, kScreenWidth, kScreenHeight)];  
  22.     if (self) {  
  23.         if (rowHeight <= 0) {  
  24.             rowHeight = 50;  
  25.         }  
  26.           
  27.         // 设置背景颜色  
  28.         self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];  
  29.         self.origin = origin;  
  30.         self.rowHeight = rowHeight;  
  31.         self.arrData = [dataArr copy];  
  32.         self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x + LeftToView, origin.y + TopToView, width, rowHeight * dataArr.count) style:UITableViewStylePlain];  
  33.         _tableView.dataSource = self;  
  34.         _tableView.delegate = self;  
  35.         [self addSubview:_tableView];  
  36.           
  37.         _tableView.backgroundColor = [UIColor whiteColor];  
  38.         _tableView.layer.cornerRadius = 2;  
  39.         _tableView.bounces = NO;  
  40.         _tableView.layer.cornerRadius = 8;  
  41.         _tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1];  
  42.         
  43.         _tableView.separatorStyle = UITableViewCellSelectionStyleNone;  
  44.         [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];  
  45.           
  46.         if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {  
  47.             [self.tableView setSeparatorInset:CellLineEdgeInsets];  
  48.         }  
  49.           
  50.         if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {  
  51.             [self.tableView setLayoutMargins:CellLineEdgeInsets];  
  52.         }  
  53.     }  
  54.     return self;  
  55. }  
  56.   
  57. - (void)layoutSubviews {  
  58.     [super layoutSubviews];  
  59. }  
  60.   
  61. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  62.     return self.arrData.count;  
  63. }  
  64.   
  65. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
  66.     return self.rowHeight;  
  67. }  
  68.   
  69. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  70.       
  71.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];  
  72.     cell.textLabel.textColor = THEME_COLOR_GRAY_1;  
  73.     cell.textLabel.font = [UIFont systemFontOfSize:15];  
  74.     cell.textLabel.text = self.arrData[indexPath.row];  
  75.       
  76.     if (self.arrImgName.count > indexPath.row) {  
  77.         cell.imageView.image = [UIImage imageNamed:self.arrImgName[indexPath.row]];  
  78.         cell.imageView.contentMode = UIViewContentModeScaleAspectFit;  
  79.     }  
  80.       
  81.     UILabel *label = [[UILabel alloc] init];  
  82.     label.frame = CGRectMake(049, _tableView.frame.size.width0.5);  
  83.     label.backgroundColor = THEME_SEPARATOR_COLOR;  
  84.     [cell.contentView addSubview:label];  
  85.      
  86.     return cell;  
  87. }  
  88.   
  89. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
  90.       
  91.     if([self.delegate respondsToSelector:@selector(OrderListDownMenu:didSelectRowAtIndexPath:)]){  
  92.         [self.delegate OrderListDownMenu:tableView didSelectRowAtIndexPath:indexPath];  
  93.     }  
  94.       
  95.     [tableView deselectRowAtIndexPath:indexPath animated:YES];  
  96.     [self dismissWithCompletion:nil];  
  97. }  
  98.   
  99. - (void)dismissWithCompletion:(void (^)(OrderListDownMenu *object))completion {  
  100.       
  101.     __weak __typeof(self) weakSelf = self;  
  102.     [UIView animateWithDuration:0.2 animations:^{  
  103.         weakSelf.alpha = 0;  
  104.         weakSelf.tableView.frame = CGRectMake(weakSelf.origin.x + LeftToView + 145, weakSelf.origin.y + TopToView, 00);  
  105.     } completion:^(BOOL finished) {  
  106.         [weakSelf removeFromSuperview];  
  107.         if (completion) {  
  108.             completion(weakSelf);  
  109.         }  
  110.         if (weakSelf.dismiss) {  
  111.             weakSelf.dismiss();  
  112.         }  
  113.     }];  
  114. }  
  115.   
  116. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {  
  117.       
  118.     UITouch *touch = [touches anyObject];  
  119.     if (![touch.view isEqual:self.tableView]) {  
  120.         [self dismissWithCompletion:nil];  
  121.     }  
  122. }  
  123.   
  124. - (void)drawRect:(CGRect)rect {  
  125.       
  126.     //[colors[serie] setFill];  
  127.       
  128.     //拿到当前视图准备好的画板  
  129.       
  130.     CGContextRef context = UIGraphicsGetCurrentContext();  
  131.       
  132.     //利用path进行绘制三角形  
  133.       
  134.     CGContextBeginPath(context);//标记  
  135.       
  136.     CGContextMoveToPoint(context,  
  137.                          rightToView - 1353);//设置起点  
  138.       
  139.     CGContextAddLineToPoint(context,  
  140.                             rightToView - 21, TopToView);  
  141.       
  142.     CGContextAddLineToPoint(context,  
  143.                             rightToView - 4, TopToView);  
  144.       
  145.     CGContextClosePath(context);//路径结束标志,不写默认封闭  
  146.       
  147.   
  148.     [self.tableView.backgroundColor setFill]; //设置填充色  
  149.       
  150.     [self.tableView.backgroundColor setStroke]; //设置边框颜色  
  151.       
  152.     CGContextDrawPath(context,  
  153.                       kCGPathFillStroke);//绘制路径path  
  154. }  
  155.   
  156. @end 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值