// // KSMenu.m // KSMenuDemo // // Created by mobao on 15/10/30. // Copyright © 2015年 mobao. All rights reserved. // #import "KSMenu.h" @implementation KSMenu - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { // self.backgroundColor = [UIColor clearColor]; self.frame = self.bounds; self.tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(self.frame.size.width-10, 60,0, 0) style:UITableViewStylePlain]; self.tableView1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"弹框底"]]; self.tableView1.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView1.delegate = self; self.tableView1.dataSource = self; self.tableView1.scrollEnabled = NO; [self addSubview:self.tableView1]; } return self; } - (void)setTitArr:(NSArray *)titArr{ [UIView animateWithDuration:0.1 animations:^{ self.tableView1.frame =CGRectMake(self.frame.size.width-160, 65,150, titArr.count * 40 + 12); }]; _titArr = titArr; // NSLog(@"----%d",_titArr.count); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _titArr.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { return 50; }else{ return 40; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"]; if (!cell) { cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; UIImageView * imageV= [[UIImageView alloc] init]; UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 300, 40)]; if (indexPath.row == 0) { imageV.frame = CGRectMake(10, 22, 20, 20); imageV.image = [UIImage imageNamed:@"新建圈子"]; lable.frame = CGRectMake(50, 10, 300, 40); }else if (indexPath.row == 1){ imageV.frame = CGRectMake(10, 10, 20, 20); imageV.image = [UIImage imageNamed:@"账号登录"]; }else if (indexPath.row == 2){ imageV.frame = CGRectMake(10, 10, 20, 20); imageV.image = [UIImage imageNamed:@"扫描圈子"]; }else if (indexPath.row == 3){ imageV.frame = CGRectMake(10, 10, 20, 20); imageV.image = [UIImage imageNamed:@"管理设备"]; }else if (indexPath.row == 4){ imageV.frame = CGRectMake(10, 10, 20, 20); imageV.image = [UIImage imageNamed:@"添加设备"]; } [cell.contentView addSubview:imageV]; lable.font = [UIFont systemFontOfSize:15]; lable.tag = 122; lable.textColor = [UIColor whiteColor]; [cell.contentView addSubview:lable]; lable.text = _titArr[indexPath.row]; } cell.backgroundColor = [UIColor clearColor]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; if ([_delegate respondsToSelector:@selector(KSMenu:)]) { [_delegate KSMenu:indexPath.row]; } [self removeFromSuperview]; } - (void)show{ AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate; [app.window addSubview:self]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self removeFromSuperview]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end