实现下拉弹出视图和Block的简单实现
2024-08-27 11:37:46
实现效果如下:
实现代码如下:
@interface ViewController ()<UIViewControllerTransitioningDelegate> { UILabel *optionLabel; //显示选中值得label BOOL isPresentingOrDismissingOption; UIView *buildingOptionView; //选择的视图 } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor greenColor]; buildingOptionView = [[UIView alloc] initWithFrame:CGRectMake(, , , )]; [self.view addSubview:buildingOptionView]; buildingOptionView.backgroundColor = [UIColor redColor]; buildingOptionView.layer.cornerRadius = ; buildingOptionView.clipsToBounds = YES; UIGestureRecognizer *tapOnBuildingOpetionView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnBuildingOpetionView:)]; [buildingOptionView addGestureRecognizer:tapOnBuildingOpetionView]; optionLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , buildingOptionView.width - , )]; optionLabel.text = @"第一教学楼"; [buildingOptionView addSubview:optionLabel]; optionLabel.textColor = [UIColor blackColor]; UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(optionLabel.right + ,optionLabel.top + ,,)]; arrowImageView.image = [UIImage imageNamed:@"classroom_arrow_icon"]; [buildingOptionView addSubview:arrowImageView]; } //手势触发方法 - (void)tapOnBuildingOpetionView:(UITapGestureRecognizer *)tap{ NSLog(@"列表选择"); //弹出选择列表 OptionTableView *optionView = [[OptionTableView alloc]initWithFrame:CGRectMake(buildingOptionView.left + ,buildingOptionView.bottom + ,,) style:UITableViewStylePlain]; optionView.buildingsArray = @[@"第一教学楼", @"第二教学楼", @"第三教学楼", @"第四教学楼", @"第五教学楼", @"第六教学楼", @"第七教学楼", @"第八教学楼"]; [self.view addSubview:optionView]; //block的实现,给label赋值 [optionView setDismissOnBuilding:^(NSString *selectedBuilding) { optionLabel.text = selectedBuilding; }]; }
OptionTableView的实现代码如下:
#import <UIKit/UIKit.h> @interface OptionTableView : UITableView<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, strong) NSArray *buildingsArray;
@property (nonatomic, copy) void (^dismissOnBuilding)(NSString *building); @end
static NSString *const buildingOptionCellId = @"Cell"; @interface OptionTableView () @end
@implementation OptionTableView - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{ self = [super initWithFrame:frame style:style];
if (self) { self.dataSource = self;
self.delegate = self; [self registerClass:[UITableViewCell class] forCellReuseIdentifier:buildingOptionCellId];
self.backgroundColor = [UIColor blueColor]; }
return self; } #pragma mark - Table view dataSource //行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{ return self.buildingsArray.count; } //创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:buildingOptionCellId forIndexPath:indexPath];
cell.textLabel.text = self.buildingsArray[indexPath.row];
return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //block实现选中的值的传递 if (self.dismissOnBuilding) {
self.dismissOnBuilding(self.buildingsArray[indexPath.row]);
} //当选择完毕表视图收起
self.frame = CGRectZero;
}
写的不好,有更好的方法,欢迎大家告诉我,谢谢!!!
最新文章
- 窥探Swift之类的继承与类的访问权限
- Chapter 2: Design the user experience
- AJAX案例一:发送POST请求
- bootstrap-列表组
- iptables基本操作
- openssl ans.1编码规则分析及证书密钥编码方式
- spring管理hibernate4 transaction getCurrentSession为什么报错?
- Access2007中如何运行SQL执行SQl语句
- CRM2011 concurrency问题及解决方案
- CodeForces 632D Longest Subsequence
- Chapter 1 First Sight——18
- 海量日志采集系统flume架构与原理
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Saratov
- jquery延迟加载(懒加载)插件
- 【Python】进程3
- ES5之defineProperty
- How to use transparent PNG icons with Delphi ImageList
- C#获取相对路径[转]
- 从 “x is null 和 x == null” 的区别看 C# 7 模式匹配中常量和 null 的匹配
- Python3 异常与断言
热门文章
- js 数组中如何删除字典
- [USACO]骑马修栅栏 Riding the Fences
- bzoj 4319: cerc2008 Suffix reconstruction 贪心
- 在默认使用apache中央仓库时, 报错 protocol_version
- yii中异步验证和自定义方法验证
- Airflow 配置celery+rabbitmq和celery+redis
- 01.Flink笔记-编译、部署
- imu 返回的数据
- Vue插槽详解
- Java核心复习——J.U.C LinkedBlockingQueue源码分析