博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
创建一个下拉控件
阅读量:5303 次
发布时间:2019-06-14

本文共 8133 字,大约阅读时间需要 27 分钟。

#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

 

转载于:https://www.cnblogs.com/CY-miaomiako/p/5387378.html

你可能感兴趣的文章
Scala入门(1)Linux下Scala(2.12.1)安装
查看>>
如何改善下面的代码 领导说了很耗资源
查看>>
Quartus II 中常见Warning 原因及解决方法
查看>>
php中的isset和empty的用法区别
查看>>
Android ViewPager 动画效果
查看>>
pip和easy_install使用方式
查看>>
博弈论
查看>>
Redis sentinel & cluster 原理分析
查看>>
我的工作习惯小结
查看>>
把word文档中的所有图片导出
查看>>
浏览器的判断;
查看>>
ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏
查看>>
Leetcode 589. N-ary Tree Preorder Traversal
查看>>
机器学习/深度学习/其他开发环境搭建记录
查看>>
xml.exist() 实例演示
查看>>
判断是否为空然后赋值
查看>>
zabbix监控日志文件
查看>>
正则表达式
查看>>
pip install torch on windows, and the 'from torch._C import * ImportError: DLL load failed:' s...
查看>>
环套树
查看>>