时间:2021-05-20
本文实例为大家分享了简单封装的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以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1、打开需要导出图片的Word文档,打开后,选择菜单栏的【文件】选项-【另存为】; 2、将文件选择路径之后另存为,在下方文件类型下拉菜单中选择【网页】,点
本文实例讲述了JS简单实现城市二级联动选择插件的方法。分享给大家供大家参考。具体如下:js实现的城市联动选择菜单,网上经常见到,不多介绍了,本款城市选择菜单原型
WPS设置二级下拉列表的方法是: 1、首先点击打开表格文件,输入需要设置为下拉列表的各项文字。选择菜单栏的公式,在属性栏点击指定按钮。 2、根据自己的情况选
使用原生选择菜单收纳导航项。它避免了导航占据大量屏幕空间。原生的选择菜单支持用户所使用设备的本地化操作,让网页交互更接近用户所使用设备的原生交互体验。原生选择菜
本文实例讲述了js实现的全国省市二级联动下拉选择菜单。分享给大家供大家参考。具体如下:运行效果截图如下:在线演示地址如下:http://demo.jb51.ne