效果如上图所示,很多APP里面都有这样的效果。
实现的大致过程:用户点击的时候,初始化一个UIView,UIView的背景色为clearcolor,然后在UIView里面添加一个tableview。
代码:
// // LrdCustomMenu.m // 加号菜单 // // Created by 键盘上的舞者 on 15/11/26. // Copyright © 2015年 键盘上的舞者. All rights reserved. // #import "LrdCustomMenu.h" #define TopToView 10.0f #define LeftToView 10.0f #define CellLineEdgeInsets UIEdgeInsetsMake(0, 10, 0, 10) #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height @interface LrdCustomMenu() @property(nonatomic,assign) CGPoint origin; @property(nonatomic,assign) CGFloat height; @end @implementation LrdCustomMenu -(instancetype)initWithDataArr:(NSArray *)dataArray origin:(CGPoint)origin width:(CGFloat)width height:(CGFloat)height { self=[super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)]; if (self) { if (height<=0) { self.height=44; } self.origin=origin; self.height=height; self.arrData=[dataArray copy]; self.backgroundColor=[UIColor clearColor]; self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(origin.x+LeftToView, origin.y+TopToView, width, height*dataArray.count) style:UITableViewStylePlain]; _tableView.delegate=self; _tableView.dataSource=self; [self addSubview:_tableView]; _tableView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1]; _tableView.layer.cornerRadius = 2; _tableView.bounces = NO; _tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1]; [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"LrdCustomMenu"]; if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparatorInset:CellLineEdgeInsets]; } if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { [self.tableView setLayoutMargins:CellLineEdgeInsets]; } } return self; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arrData.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return self.height; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LrdCustomMenu"]; cell.backgroundColor = [UIColor clearColor]; cell.textLabel.textColor = [UIColor whiteColor]; 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.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame]; cell.selectedBackgroundView.backgroundColor = [UIColor blackColor]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.delegate respondsToSelector:@selector(LrdCustomMenu:didSelectedAtIndexPath:)]) { [self.delegate LrdCustomMenu:tableView didSelectedAtIndexPath:indexPath]; } [tableView deselectRowAtIndexPath:indexPath animated:YES]; [self dissmissWithCompletion:nil]; } -(void)dissmissWithCompletion:(void (^)(LrdCustomMenu *))completion { __weak __typeof(self) weakSelf = self; [UIView animateWithDuration:0.2 animations:^{ weakSelf.alpha = 0; weakSelf.tableView.frame = CGRectMake(weakSelf.origin.x + LeftToView, 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 dissmissWithCompletion:nil]; } } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:CellLineEdgeInsets]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:CellLineEdgeInsets]; } } - (void)drawRect:(CGRect)rect { // [colors[serie] setFill]; //拿到当前视图准备好的画板 CGContextRef context = UIGraphicsGetCurrentContext(); //利用path进行绘制三角形 CGContextBeginPath(context);//标记 CGContextMoveToPoint(context, LeftToView * 2.5, TopToView * 0.5);//设置起点 CGContextAddLineToPoint(context, LeftToView * 2, TopToView); CGContextAddLineToPoint(context, LeftToView * 3, TopToView); CGContextClosePath(context);//路径结束标志,不写默认封闭 [self.tableView.backgroundColor setFill]; //设置填充色 // [self.tableView.backgroundColor setStroke]; //设置边框颜色 CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path } @end
// // LrdCustomMenu.h // 加号菜单 // // Created by 键盘上的舞者 on 15/11/26. // Copyright © 2015年 键盘上的舞者. All rights reserved. // #import <UIKit/UIKit.h> @protocol LrdCustomMenuDelegate <NSObject> @optional -(void)LrdCustomMenu:(UITableView *)tableview didSelectedAtIndexPath:(NSIndexPath *)indexPath; @end typedef void(^Dismiss)(void); @interface LrdCustomMenu : UIView<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, assign) id<LrdCustomMenuDelegate> delegate; @property (nonatomic, strong) NSArray *arrData; @property (nonatomic, strong) NSArray *arrImgName; @property (nonatomic, copy) Dismiss dismiss; //初始化类方法 -(instancetype)initWithDataArr:(NSArray *)dataArray origin:(CGPoint)origin width:(CGFloat)width height:(CGFloat)height; //销毁的时候的Block -(void)dissmissWithCompletion:(void (^)(LrdCustomMenu *obj))completion; @end
// // ViewController.m // 加号菜单 // // Created by 键盘上的舞者 on 15/11/26. // Copyright © 2015年 键盘上的舞者. All rights reserved. // #import "ViewController.h" #import "LrdCustomMenu.h" @interface ViewController ()<LrdCustomMenuDelegate> @property(nonatomic,strong) LrdCustomMenu *menu; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.translucent=NO; //添加左侧的item UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"schoolListItem"] style:UIBarButtonItemStylePlain target:self action:@selector(showMenu:)]; self.navigationItem.leftBarButtonItem=item; } -(void)showMenu:(UIBarButtonItem *)buttonItem { __weak __typeof(self) weakSelf = self; if (!self.menu) { self.menu = [[LrdCustomMenu alloc] initWithDataArr:@[@"附近学校", @"联赛流程", @"其他联赛", @"校内群聊", @"邀请好友"] origin:CGPointMake(0, 0) width:125 height:44]; _menu.delegate = self; _menu.dismiss = ^() { weakSelf.menu = nil; }; _menu.arrImgName = @[@"item_school", @"item_battle", @"item_list", @"item_chat", @"item_share"]; [self.view addSubview:_menu]; } else { [_menu dissmissWithCompletion:^(LrdCustomMenu *object) { weakSelf.menu = nil; }]; } } -(void)LrdCustomMenu:(UITableView *)tableview didSelectedAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"select: %ld", indexPath.row); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end