iOS下拉選擇菜單簡(jiǎn)單封裝
本文實(shí)例為大家分享了簡(jiǎn)單封裝的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)備好的畫(huà)板
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)志,不寫(xiě)默認(rèn)封閉
[self.tableView.backgroundColor setFill]; //設(shè)置填充色
[self.tableView.backgroundColor setStroke]; //設(shè)置邊框顏色
CGContextDrawPath(context,
kCGPathFillStroke);//繪制路徑path
}
@end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- iOS 三級(jí)下拉菜單功能實(shí)現(xiàn)
- iOS10 widget實(shí)現(xiàn)3Dtouch 彈出菜單
- iOS實(shí)現(xiàn)簡(jiǎn)單的二級(jí)菜單效果
- IOS中safari下的select下拉菜單文字過(guò)長(zhǎng)不換行的解決方法
- IOS代碼筆記之下拉菜單效果
- iOS中長(zhǎng)按調(diào)出菜單組件UIMenuController的使用實(shí)例
- iOS從App跳轉(zhuǎn)至系統(tǒng)設(shè)置菜單各功能項(xiàng)的編寫(xiě)方法講解
- iOS實(shí)現(xiàn)頂部標(biāo)簽式導(dǎo)航欄及下拉分類菜單
- 如何使用jQuery技術(shù)開(kāi)發(fā)ios風(fēng)格的頁(yè)面導(dǎo)航菜單
- iOS實(shí)現(xiàn)Pad上菜單彈出界面
相關(guān)文章
iOS App使用GCD導(dǎo)致的卡頓現(xiàn)象及解決方法
這篇文章主要給大家介紹了關(guān)于iOS App使用GCD導(dǎo)致的卡頓現(xiàn)象及解決方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
iOS表視圖之下拉刷新控件功能的實(shí)現(xiàn)方法
下拉刷新是重新刷新表視圖或列表,以便重新加載數(shù)據(jù),這種模式廣泛用于移動(dòng)平臺(tái),相信大家對(duì)于此也是非常熟悉的,那么iOS是如何做到的下拉刷新呢?下面小編給大家分享iOS表視圖之下拉刷新控件的實(shí)現(xiàn)方法,一起看看吧2017-01-01
Xcode8以及iOS10適配等常見(jiàn)問(wèn)題匯總(整理篇)
隨著iOS 10的更新以及Xcdoe 8的更新出現(xiàn)了很多問(wèn)題,今天小編抽時(shí)間給大家整理下我遇到的坑特此分享到腳本之家平臺(tái),供大家參考2016-09-09
實(shí)例分析IOS實(shí)現(xiàn)自動(dòng)打包
本篇文章給大家分享了IOS實(shí)現(xiàn)自動(dòng)打包的相關(guān)知識(shí)點(diǎn),以及需要的操作內(nèi)容做了分享,有需要的朋友可以學(xué)習(xí)下。2018-05-05
iOS應(yīng)用中發(fā)送HTTP的get請(qǐng)求以及HTTP異步請(qǐng)求的方法
這篇文章主要介紹了iOS應(yīng)用中發(fā)送HTTP的get請(qǐng)求以及HTTP異步請(qǐng)求的方法,代碼為傳統(tǒng)的Objective-C語(yǔ)言,說(shuō)明都簡(jiǎn)單地融入于注釋之中,需要的朋友可以參考下2016-02-02
IOS 實(shí)現(xiàn)一個(gè)死鎖導(dǎo)致 UI 假死的例子
這篇文章主要介紹了IOS 實(shí)現(xiàn)一個(gè)死鎖導(dǎo)致 UI 假死的例子的相關(guān)資料,需要的朋友可以參考下2016-12-12
iOS 修改alertViewController彈框的字體顏色及字體的方法
下面小編就為大家分享一篇iOS 修改alertViewController彈框的字體顏色及字體的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
iOS中指紋識(shí)別常見(jiàn)問(wèn)題匯總
最近在公司做了一個(gè)app要使用指紋支付的功能,在實(shí)現(xiàn)過(guò)程中遇到各種坑,今天小編抽抗給大家總結(jié)把遇到問(wèn)題匯總特此分享到腳本之家平臺(tái),需要的朋友參考下2016-12-12

