国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS下拉選擇菜單簡單封裝

2019-10-21 18:51:06
字體:
來源:轉載
供稿:網友

本文實例為大家分享了簡單封裝的iOS下拉選擇菜單代碼,供大家參考,具體內容如下

// // OrderListDownMenu.h  #import <UIKit/UIKit.h>  @protocol OrderListDownMenuDelegate <NSObject>  - (void)OrderListDownMenu:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;  @end  typedef void(^Dismiss)(void);  @interface OrderListDownMenu : UIView<UITableViewDataSource, UITableViewDelegate>  @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, assign) id<OrderListDownMenuDelegate> delegate; @property (nonatomic, strong) NSArray *arrData; @property (nonatomic, strong) NSArray *arrImgName; @property (nonatomic, copy) Dismiss dismiss;  - (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight;  - (void)dismissWithCompletion:(void (^)(OrderListDownMenu *object))completion;  @end 
#import "OrderListDownMenu.h"  #define TopToView 63.0f #define rightToView kScreenWidth - 15.0f #define LeftToView kScreenWidth - 145.0 - 10.0f #define CellLineEdgeInsets UIEdgeInsetsMake(0, -80, 0, 0) #define kScreenWidth    [UIScreen mainScreen].bounds.size.width #define kScreenHeight    [UIScreen mainScreen].bounds.size.height  @interface OrderListDownMenu()  @property (nonatomic, assign) CGPoint origin; @property (nonatomic, assign) CGFloat rowHeight;  @end  @implementation OrderListDownMenu  - (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight {      self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];   if (self) {     if (rowHeight <= 0) {       rowHeight = 50;     }          // 設置背景顏色     self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];     self.origin = origin;     self.rowHeight = rowHeight;     self.arrData = [dataArr copy];     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x + LeftToView, origin.y + TopToView, width, rowHeight * dataArr.count) style:UITableViewStylePlain];     _tableView.dataSource = self;     _tableView.delegate = self;     [self addSubview:_tableView];          _tableView.backgroundColor = [UIColor whiteColor];     _tableView.layer.cornerRadius = 2;     _tableView.bounces = NO;     _tableView.layer.cornerRadius = 8;     _tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1];         _tableView.separatorStyle = UITableViewCellSelectionStyleNone;     [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];          if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {       [self.tableView setSeparatorInset:CellLineEdgeInsets];     }          if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {       [self.tableView setLayoutMargins:CellLineEdgeInsets];     }   }   return self; }  - (void)layoutSubviews {   [super layoutSubviews]; }  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   return self.arrData.count; }  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {   return self.rowHeight; }  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];   cell.textLabel.textColor = THEME_COLOR_GRAY_1;   cell.textLabel.font = [UIFont systemFontOfSize:15];   cell.textLabel.text = self.arrData[indexPath.row];      if (self.arrImgName.count > indexPath.row) {     cell.imageView.image = [UIImage imageNamed:self.arrImgName[indexPath.row]];     cell.imageView.contentMode = UIViewContentModeScaleAspectFit;   }      UILabel *label = [[UILabel alloc] init];   label.frame = CGRectMake(0, 49, _tableView.frame.size.width, 0.5);   label.backgroundColor = THEME_SEPARATOR_COLOR;   [cell.contentView addSubview:label];      return cell; }  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {      if([self.delegate respondsToSelector:@selector(OrderListDownMenu:didSelectRowAtIndexPath:)]){     [self.delegate OrderListDownMenu:tableView didSelectRowAtIndexPath:indexPath];   }      [tableView deselectRowAtIndexPath:indexPath animated:YES];   [self dismissWithCompletion:nil]; }  - (void)dismissWithCompletion:(void (^)(OrderListDownMenu *object))completion {      __weak __typeof(self) weakSelf = self;   [UIView animateWithDuration:0.2 animations:^{     weakSelf.alpha = 0;     weakSelf.tableView.frame = CGRectMake(weakSelf.origin.x + LeftToView + 145, weakSelf.origin.y + TopToView, 0, 0);   } completion:^(BOOL finished) {     [weakSelf removeFromSuperview];     if (completion) {       completion(weakSelf);     }     if (weakSelf.dismiss) {       weakSelf.dismiss();     }   }]; }  - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {      UITouch *touch = [touches anyObject];   if (![touch.view isEqual:self.tableView]) {     [self dismissWithCompletion:nil];   } }  - (void)drawRect:(CGRect)rect {      //[colors[serie] setFill];      //拿到當前視圖準備好的畫板      CGContextRef context = UIGraphicsGetCurrentContext();      //利用path進行繪制三角形      CGContextBeginPath(context);//標記      CGContextMoveToPoint(context,              rightToView - 13, 53);//設置起點      CGContextAddLineToPoint(context,               rightToView - 21, TopToView);      CGContextAddLineToPoint(context,               rightToView - 4, TopToView);      CGContextClosePath(context);//路徑結束標志,不寫默認封閉       [self.tableView.backgroundColor setFill]; //設置填充色      [self.tableView.backgroundColor setStroke]; //設置邊框顏色      CGContextDrawPath(context,            kCGPathFillStroke);//繪制路徑path }  @end 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 弥渡县| 囊谦县| 清新县| 桃园市| 武邑县| 漾濞| 昌邑市| 沛县| 金门县| 远安县| 铜陵市| 合肥市| 长葛市| 南靖县| 阿克陶县| 从化市| 凤冈县| 桐柏县| 江陵县| 博客| 郁南县| 大渡口区| 连江县| 浠水县| 高碑店市| 夏河县| 怀柔区| 洪洞县| 南通市| 塔城市| 改则县| 新兴县| 翁牛特旗| 东宁县| 巴塘县| 鄢陵县| 武城县| 黔西县| 铜山县| 衡阳县| 中牟县|