iOS下拉选择菜单简单封装

所属分类: 软件编程 / IOS 阅读数: 742
收藏 0 赞 0 分享

本文实例为大家分享了简单封装的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 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

iOS 仿百度外卖-首页重力感应的实例

这篇文章主要介绍了iOS 仿百度外卖-首页重力感应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

iOS实现时间显示几分钟前,几小时前以及刚刚的方法示例

这篇文章主要介绍了如何利用iOS实现时间显示是在几小时前,几分钟前以及刚刚的格式,类似大家在qq空间和朋友圈微博看到的效果,文中给出了详细的示例代码,有需要的朋友们可以参考借鉴,下面来一起学习学习吧。
收藏 0 赞 0 分享

iOS 条码及二维码扫描(从相册中读取条形码/二维码)及扫码过程中遇到的坑

本文主要给大家介绍ios中从手机相册中读取条形码和二维码的问题及解决办法,需要的朋友参考下
收藏 0 赞 0 分享

IOS Cache设计详细介绍及简单示例

这篇文章主要介绍了IOS Cache设计详细介绍及简单示例的相关资料,Cache的目的是为了追求更高的速度体验,Cache的源头是两种数据读取方式在成本和性能上的差异,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS本地动态生成验证码的方法

这篇文章主要介绍了iOS本地动态生成验证码的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS绘制3D饼图的实现方法

饼图常用于统计学模块。常见的一般为2D饼图,这篇文章主要介绍了iOS绘制3D饼图的实现方法,3D饼图更加立体,用户的好感度也比较高,下面需要的朋友可以参考借鉴,一起来看看吧。
收藏 0 赞 0 分享

谈谈iOS开发之JSON格式数据的生成与解析

JSON格式取代了xml给网络传输带来了很大的便利,本篇文章主要介绍了iOS开发:对象直接转化成JSON详解,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

IOS 身份证校验详细介绍及示例代码

这篇文章主要介绍了IOS 身份证校验详细介绍及示例代码的相关资料,这里对身份校验比较详细,附有简单实例,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView

这篇文章主要介绍了IOS 自定义UICollectionView的头视图或者尾视图UICollectionReusableView的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS 仿支付宝支付屏幕亮度变化机制

这篇文章主要介绍了IOS 仿支付宝支付屏幕亮度变化机制的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多