iOS下拉選擇菜單簡單封裝
本文實(shí)例為大家分享了簡單封裝的iOS下拉選擇菜單代碼,供大家參考,具體內(nèi)容如下
// // 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;
}
// 設(shè)置背景顏色
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];
//拿到當(dāng)前視圖準(zhǔn)備好的畫板
CGContextRef context = UIGraphicsGetCurrentContext();
//利用path進(jìn)行繪制三角形
CGContextBeginPath(context);//標(biāo)記
CGContextMoveToPoint(context,
rightToView - 13, 53);//設(shè)置起點(diǎn)
CGContextAddLineToPoint(context,
rightToView - 21, TopToView);
CGContextAddLineToPoint(context,
rightToView - 4, TopToView);
CGContextClosePath(context);//路徑結(jié)束標(biāo)志,不寫默認(rèn)封閉
[self.tableView.backgroundColor setFill]; //設(shè)置填充色
[self.tableView.backgroundColor setStroke]; //設(shè)置邊框顏色
CGContextDrawPath(context,
kCGPathFillStroke);//繪制路徑path
}
@end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS 三級下拉菜單功能實(shí)現(xiàn)
- iOS10 widget實(shí)現(xiàn)3Dtouch 彈出菜單
- iOS實(shí)現(xiàn)簡單的二級菜單效果
- IOS中safari下的select下拉菜單文字過長不換行的解決方法
- IOS代碼筆記之下拉菜單效果
- iOS中長按調(diào)出菜單組件UIMenuController的使用實(shí)例
- iOS從App跳轉(zhuǎn)至系統(tǒng)設(shè)置菜單各功能項(xiàng)的編寫方法講解
- iOS實(shí)現(xiàn)頂部標(biāo)簽式導(dǎo)航欄及下拉分類菜單
- 如何使用jQuery技術(shù)開發(fā)ios風(fēng)格的頁面導(dǎo)航菜單
- iOS實(shí)現(xiàn)Pad上菜單彈出界面
相關(guān)文章
iOS應(yīng)用開發(fā)中使用設(shè)計(jì)模式中的觀察者模式的實(shí)例
這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用設(shè)計(jì)模式中的觀察者模式的實(shí)例,包括Cocoa框架使用中的KVO機(jī)制的相關(guān)配合運(yùn)用,代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法
這篇文章主要為大家詳細(xì)介紹了UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
在iOS中使用OpenGL ES實(shí)現(xiàn)繪畫板的方法
這篇文章主要介紹了在iOS中使用OpenGL ES實(shí)現(xiàn)繪畫板的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Xcode中iOS應(yīng)用開發(fā)的一般項(xiàng)目目錄結(jié)構(gòu)和流程簡介
這篇文章主要介紹了Xcode中iOS應(yīng)用開發(fā)的一般項(xiàng)目目錄結(jié)構(gòu)和流程簡介,包括項(xiàng)目所需的一些平臺(tái)路徑如模擬器路徑等的介紹,需要的朋友可以參考下2016-02-02
詳解iOS應(yīng)用中播放本地視頻以及選取本地音頻的組件用法
這里來為大家詳解iOS應(yīng)用中播放本地視頻以及選取本地音頻的組件用法,分別使用MPMoviePlayerControlle和MPMediaPickerController來實(shí)現(xiàn),兩個(gè)都是MediaPlayer.framework中的多媒體組件,所以我們放到一起來講.2016-06-06
iOS項(xiàng)目開發(fā)鍵盤彈出遮擋輸入框問題解決方案
大家在用IOS開發(fā)項(xiàng)目的時(shí)候,經(jīng)常出現(xiàn)鍵盤彈出遮擋輸入框問題,小編給大家整理的這個(gè)問題的處理方法,一起學(xué)習(xí)下。2018-01-01
iOS ScrollView實(shí)現(xiàn)自動(dòng)布局的方法(適用Swift 3.0 )
傳說中有一個(gè)美工ios開發(fā)者在遇到這個(gè)問題的時(shí)候特意跑到蘋果總部去咨詢?nèi)绾螌crollview進(jìn)行自動(dòng)布局。當(dāng)然大家不用去了,下面這篇文章就來給大家介紹關(guān)于iOS ScrollView實(shí)現(xiàn)自動(dòng)布局的方法,文中的語法同樣也適用Swift 3.0 ,需要的朋友可以參考下。2017-12-12
ios開發(fā)navigationController pushViewController 方式多次跳轉(zhuǎn)返回到最上層返回到
這篇文章主要介紹了ios開發(fā)navigationController pushViewController 方式多次跳轉(zhuǎn)返回到最上層返回到指定的某一層的實(shí)現(xiàn)方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
IOS 屏幕適配方案實(shí)現(xiàn)縮放window的示例代碼
這篇文章主要介紹了IOS 屏幕適配方案實(shí)現(xiàn)縮放window的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

