欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS下拉选择菜单简单封装

程序员文章站 2024-02-12 14:14:04
本文实例为大家分享了简单封装的ios下拉选择菜单代码,供大家参考,具体内容如下 // // orderlistdownmenu.h #import...

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

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