#import@interface LCPellTableViewSelect : UIView/** * 创建一个弹出下拉控件 * * @param frame 尺寸 * @param selectData 选择控件的数据源 * @param action 点击回调方法 * @param animate 是否动画弹出 */+ (void)addPellTableViewSelectWithWindowFrame:(CGRect)frame selectData:(NSArray *)selectData images:(NSArray *)images action:(void(^)(NSIndexPath *indexPath))action animated:(BOOL)animate;/** * 手动隐藏 */+ (void)hiden;@end#import "LCPellTableViewSelect.h"#import "LCClickCell.h"#define LeftView 10.0f#define TopToView 10.0f@interface LCPellTableViewSelect() @property (nonatomic,copy) NSArray *selectData;@property (nonatomic,copy) void(^action)(NSIndexPath * indexPath);@property (nonatomic,copy) NSArray * imagesData;@endLCPellTableViewSelect * backgroundView;UITableView * tableView;UIImageView* image;@implementation LCPellTableViewSelect- (instancetype)init{ if (self = [super init]) { } return self;}- (instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) { } return self;}- (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { } return self;}+ (void)addPellTableViewSelectWithWindowFrame:(CGRect)frame selectData:(NSArray *)selectData images:(NSArray *)images action:(void(^)(NSIndexPath * indexPath))action animated:(BOOL)animate{ if (backgroundView != nil) { [LCPellTableViewSelect hiden]; } UIWindow *win = [[[UIApplication sharedApplication] windows] firstObject]; backgroundView = [[LCPellTableViewSelect alloc] initWithFrame:win.bounds]; backgroundView.action = action; backgroundView.imagesData = images ; backgroundView.selectData = selectData; backgroundView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:0.3]; [win addSubview:backgroundView]; // TAB tableView = [[UITableView alloc] initWithFrame:CGRectMake( [UIScreen mainScreen].bounds.size.width - 74 , 70.0 - 20.0 * selectData.count+10, frame.size.width-10, 40 * selectData.count-40) style:UITableViewStyleGrouped]; NSLog( @"%fl",frame.size.width); tableView.dataSource = backgroundView; // tableView.transform = CGAffineTransformMakeScale(0.5, 0.5); tableView.delegate = backgroundView; tableView.layer.cornerRadius = 10.0f; // 定点 tableView.layer.anchorPoint = CGPointMake(1.0, 0); tableView.transform =CGAffineTransformMakeScale(0.0001, 0.0001); tableView.separatorInset=UIEdgeInsetsMake(0, -10, 0, 0); tableView.rowHeight = 40; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.scrollEnabled = NO; [win addSubview:tableView]; image=[[UIImageView alloc]init]; image.image=[UIImage imageNamed:@"xiaosanjiao"]; image.frame=CGRectMake(tableView.frame.origin.x-34, tableView.frame.origin.y-7, 15, 8); [win addSubview:image]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackgroundClick)]; [backgroundView addGestureRecognizer:tap]; backgroundView.action = action; backgroundView.selectData = selectData; if (animate == YES) { backgroundView.alpha = 0; [UIView animateWithDuration:0.3 animations:^{ backgroundView.alpha = 0.5; tableView.transform = CGAffineTransformMakeScale(1.0, 1.0); }]; }}+ (void)tapBackgroundClick{ [LCPellTableViewSelect hiden]; }+ (void)hiden{ if (backgroundView != nil) { [UIView animateWithDuration:0.32 animations:^{ tableView.transform = CGAffineTransformMakeScale(0.000001, 0.0001); [tableView removeFromSuperview]; [image removeFromSuperview]; tableView = nil; } completion:^(BOOL finished) { [backgroundView removeFromSuperview]; [image removeFromSuperview]; // [tableView removeFromSuperview]; // tableView = nil; backgroundView = nil; }]; } }//几个分区-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3;}//每个分区有几行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 2) { return 1; } return 2; }-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 2;}-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.1;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 40;}//配置cell- (LCClickCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *Identifier = @"PellTableViewSelectIdentifier"; LCClickCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; if (cell == nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"LCClickCell" owner:self options:nil]objectAtIndex:0]; } cell.imageView.frame=CGRectMake(5, 0, 20, 20); cell.textLabel.font=[UIFont fontWithName:@"Helvetica" size:17]; cell.textLabel.frame=CGRectMake(cell.imageView.frame.origin.x+cell.imageView.bounds.size.width+5, 0, 80, 50); cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.section==0&&indexPath.row==0) { cell.imageView.image=[UIImage imageNamed:@"xinpengyou"]; cell.textLabel.text=self.selectData[0]; }if (indexPath.section==0&&indexPath.row==1) { cell.imageView.image=[UIImage imageNamed:@"xinqunliao"]; cell.textLabel.text=self.selectData[1]; }if (indexPath.section==1&&indexPath.row==0) { cell.imageView.image=[UIImage imageNamed:@"fujinderen"]; cell.textLabel.text=self.selectData[2]; }if (indexPath.section==1&&indexPath.row==1) { cell.imageView.image=[UIImage imageNamed:@"fujindequn"]; cell.textLabel.text=self.selectData[4]; }if (indexPath.section==2&&indexPath.row==0) { cell.imageView.image=[UIImage imageNamed:@"saoyisao"]; cell.textLabel.text=self.selectData[5]; } // cell.imageView.image = [UIImage imageNamed:self.imagesData[indexPath.row]]; // cell.textLabel.text = _selectData[indexPath.row]; return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (self.action) { self.action(indexPath); } [LCPellTableViewSelect hiden];}@end
// 自定义Cell
#import <UIKit/UIKit.h>
@interface LCClickCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *name;
@end
#import "ViewController.h"#import "LCPellTableViewSelect.h"#import "NewFriendViewController.h"@interface ViewController ()- (IBAction)Btn:(id)sender;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (IBAction)Btn:(id)sender { NSLog(@"666666"); [LCPellTableViewSelect addPellTableViewSelectWithWindowFrame:CGRectMake(self.view.bounds.size.width-100, 64, 150, 200) selectData:@[@"新朋友",@"新群聊",@"附近的人",@"附近的人",@"附近的群",@"扫一扫"] images:@[@"xinpengyou",@"xinqunliao",@"fujinderen",@"fujindequn",@"fujindequn",@"saoyisao"] action:^(NSIndexPath *indexPath) { if (indexPath.section==0&&indexPath.row==0) { NewFriendViewController *newfriends=[[NewFriendViewController alloc]init]; [self.navigationController pushViewController:newfriends animated:YES]; } } animated:YES]; }@end