iOS下拉选择菜单简单封装

所属分类: 软件编程 / IOS 阅读数: 753
收藏 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开发相册图片多选和删除的功能

之前小编有和大家分享过一篇关于从相册选取单张照片的文章,那么下面这篇文章跟大家分享下如何相册多图选择和删除,以及包括拍照功能,有需要的可以参考学习,下面来一起看看吧。
收藏 0 赞 0 分享

iOS使用runtime修改文本框(TextField)的占位文字颜色

相信大家都知道TextField默认的占位颜色也是深灰色,这个颜色比较难看清,这篇文章给大家介绍如何使用runtime修改TextField文本框的占位文字颜色,有需要的可以参考借鉴.
收藏 0 赞 0 分享

iOS实现点击状态栏自动回到顶部效果详解

在IOS开发过程中,经常会有这种需求,需要通过点击状态栏返回到顶部,给用户更好的体验效果,下面这篇文章给大家详细介绍了实现过程,有需要的可以参考借鉴。
收藏 0 赞 0 分享

IOS上iframe的滚动条失效的解决办法

这篇文章主要为大家详细介绍了IOS上iframe的滚动条失效的解决办法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

IOS面试大全之常见算法

之前看了很多面试题,感觉要不是不够就是过于冗余,于是我将网上的一些面试题进行了删减和分类,这篇文章先给大家分享一下IOS中的常见算法,有需要的可以参考借鉴。
收藏 0 赞 0 分享

IOS判断字符串是否有空格实例

在我们大家日常开发的时候,经常会需要对注册,登录,忘记密码等功能的密码进行判断是否包含空格,下面这篇文章给大家分享了自己封装的一个方法,有需要的可以参考借鉴。
收藏 0 赞 0 分享

IOS设置按钮为圆角的示例代码

这篇文章给大家分享了IOS按钮设置为圆角的方法,按钮的四个角都可随意设置为圆角,对大家开发IOS具有一定的参考借鉴价值。有需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

IOS绘制虚线的方法总结

这篇文章给大家分享了iOS中绘制虚线常见的几种方式,大家可以根据自己的需求进行选择哪种方法,下面跟着小编来一起看看吧。
收藏 0 赞 0 分享

React Native搭建iOS开发环境

React Native的门槛不管是对于前端开发者还是移动端开发者来说都是很高的,既要懂原生又要懂js,技术栈是相当长的。但是没有关系,下面我们一步步来学习,慢慢成长吧!
收藏 0 赞 0 分享

IOS轻松几步实现自定义转场动画

这篇文章将讲述几个步骤实现转场动画的自定义方式,并且给出了示例代码,毕竟代码才是我们的语言,这样比较容易上手。下面来一起看看吧。
收藏 0 赞 0 分享
查看更多