iOS下拉选择菜单简单封装
本文实例为大家分享了简单封装的iOS下拉选择菜单代码,供大家参考,具体内容如下
// //OrderListDownMenu.h #import<UIKit/UIKit.h> @protocolOrderListDownMenuDelegate<NSObject> -(void)OrderListDownMenu:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath; @end typedefvoid(^Dismiss)(void); @interfaceOrderListDownMenu: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)Dismissdismiss; -(instancetype)initWithDataArr:(NSArray*)dataArrorigin:(CGPoint)originwidth:(CGFloat)widthrowHeight:(CGFloat)rowHeight; -(void)dismissWithCompletion:(void(^)(OrderListDownMenu*object))completion; @end
#import"OrderListDownMenu.h" #defineTopToView63.0f #definerightToViewkScreenWidth-15.0f #defineLeftToViewkScreenWidth-145.0-10.0f #defineCellLineEdgeInsetsUIEdgeInsetsMake(0,-80,0,0) #definekScreenWidth[UIScreenmainScreen].bounds.size.width #definekScreenHeight[UIScreenmainScreen].bounds.size.height @interfaceOrderListDownMenu() @property(nonatomic,assign)CGPointorigin; @property(nonatomic,assign)CGFloatrowHeight; @end @implementationOrderListDownMenu -(instancetype)initWithDataArr:(NSArray*)dataArrorigin:(CGPoint)originwidth:(CGFloat)widthrowHeight:(CGFloat)rowHeight{ self=[superinitWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight)]; if(self){ if(rowHeight<=0){ rowHeight=50; } //设置背景颜色 self.backgroundColor=[UIColorcolorWithRed:0green:0blue:0alpha:0.2]; self.origin=origin; self.rowHeight=rowHeight; self.arrData=[dataArrcopy]; self.tableView=[[UITableViewalloc]initWithFrame:CGRectMake(origin.x+LeftToView,origin.y+TopToView,width,rowHeight*dataArr.count)style:UITableViewStylePlain]; _tableView.dataSource=self; _tableView.delegate=self; [selfaddSubview:_tableView]; _tableView.backgroundColor=[UIColorwhiteColor]; _tableView.layer.cornerRadius=2; _tableView.bounces=NO; _tableView.layer.cornerRadius=8; _tableView.separatorColor=[UIColorcolorWithWhite:0.3alpha:1]; _tableView.separatorStyle=UITableViewCellSelectionStyleNone; [_tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"cell"]; if([self.tableViewrespondsToSelector:@selector(setSeparatorInset:)]){ [self.tableViewsetSeparatorInset:CellLineEdgeInsets]; } if([self.tableViewrespondsToSelector:@selector(setLayoutMargins:)]){ [self.tableViewsetLayoutMargins:CellLineEdgeInsets]; } } returnself; } -(void)layoutSubviews{ [superlayoutSubviews]; } -(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{ returnself.arrData.count; } -(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{ returnself.rowHeight; } -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{ UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:@"cell"]; cell.textLabel.textColor=THEME_COLOR_GRAY_1; cell.textLabel.font=[UIFontsystemFontOfSize:15]; cell.textLabel.text=self.arrData[indexPath.row]; if(self.arrImgName.count>indexPath.row){ cell.imageView.image=[UIImageimageNamed:self.arrImgName[indexPath.row]]; cell.imageView.contentMode=UIViewContentModeScaleAspectFit; } UILabel*label=[[UILabelalloc]init]; label.frame=CGRectMake(0,49,_tableView.frame.size.width,0.5); label.backgroundColor=THEME_SEPARATOR_COLOR; [cell.contentViewaddSubview:label]; returncell; } -(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{ if([self.delegaterespondsToSelector:@selector(OrderListDownMenu:didSelectRowAtIndexPath:)]){ [self.delegateOrderListDownMenu:tableViewdidSelectRowAtIndexPath:indexPath]; } [tableViewdeselectRowAtIndexPath:indexPathanimated:YES]; [selfdismissWithCompletion:nil]; } -(void)dismissWithCompletion:(void(^)(OrderListDownMenu*object))completion{ __weak__typeof(self)weakSelf=self; [UIViewanimateWithDuration:0.2animations:^{ weakSelf.alpha=0; weakSelf.tableView.frame=CGRectMake(weakSelf.origin.x+LeftToView+145,weakSelf.origin.y+TopToView,0,0); }completion:^(BOOLfinished){ [weakSelfremoveFromSuperview]; if(completion){ completion(weakSelf); } if(weakSelf.dismiss){ weakSelf.dismiss(); } }]; } -(void)touchesBegan:(NSSet<UITouch*>*)toucheswithEvent:(UIEvent*)event{ UITouch*touch=[touchesanyObject]; if(![touch.viewisEqual:self.tableView]){ [selfdismissWithCompletion:nil]; } } -(void)drawRect:(CGRect)rect{ //[colors[serie]setFill]; //拿到当前视图准备好的画板 CGContextRefcontext=UIGraphicsGetCurrentContext(); //利用path进行绘制三角形 CGContextBeginPath(context);//标记 CGContextMoveToPoint(context, rightToView-13,53);//设置起点 CGContextAddLineToPoint(context, rightToView-21,TopToView); CGContextAddLineToPoint(context, rightToView-4,TopToView); CGContextClosePath(context);//路径结束标志,不写默认封闭 [self.tableView.backgroundColorsetFill];//设置填充色 [self.tableView.backgroundColorsetStroke];//设置边框颜色 CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。