下拉列表的实现方法有许多种,笔者在此只介绍实际上目中切实使用的,最简单(并不是最优)的一种方法。
实现思路:
1、自定义一个UIView,添加到相应ViewController的指定位置。默认透明度为0。
2、给对应的调出控件添加相应方法,在相应方法中将下拉列表的透明度设为1。
3、为了达到下拉列表调出的时候,其他控件不响应,在下拉列表下添加一个UIView,添加到同样的ViewController 上,默认透明度也为0。在下拉列表调出的时候,同时设置其透明度为0.3(根据需要调整)。
4、再次点击调出控件的时候或者是点击下拉列表中的控件时,设置下拉列表,及其遮挡视图的透明度为0。(点击调出控件的响应方法中可以设置一个全局的Bool值,每次点击都变为 !Bool 值)
下面是实现代码。
(1)、自定义的下拉列表的UIView
#import "BView.h"
@interface testView : BView
@property (nonatomic, copy) void(^searchTypeVCBlock)(NSInteger);
@end
#import "testView.h"
#import "Masonry.h"
@implementation testView
-(id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
NSArray * arr = @[@"全部",@"商品",@"方案"];
for (int i=0; i<arr.count; i++) {
UIButton * button = [[UIButton alloc] init];
[button setTitle:arr[i] forState:UIControlStateNormal];
[button setTitleColor:UIColorFromRGBOne(0x73) forState:UIControlStateNormal];
button.titleLabel.font = GetFont(BFONT_15);
button.tag = 100+i;
[button addTarget:self action:@selector(clickbutton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
button.frame = CGRectMake(0, i*35, SCREEN_WIDTH, 35);
}
for (int j=0; j<2; j++) {
UIImageView * testImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 35+j*35, SCREEN_WIDTH, 1)];
testImageView.backgroundColor = UIColorFromRGBOne(0x99);
[self addSubview:testImageView];
}
}
return self;
}
-(void)clickbutton:(UIButton *)button{
if (button.tag == 100) {
if (self.searchTypeVCBlock){
self.searchTypeVCBlock(0);
}
}else if (button.tag == 101){
if (self.searchTypeVCBlock){
self.searchTypeVCBlock(1);
}
}else if (button.tag == 102){
if (self.searchTypeVCBlock){
self.searchTypeVCBlock(2);
}
}
// NSInteger typeTag = button.tag-100;
// self.searchTypeVCBlock(typeTag);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
(2)、在对应的ViewController中添加UIVIew
_KeepOutView = [[UIView alloc]initWithFrame:self.view.frame];
_KeepOutView.backgroundColor = [UIColor grayColor];
_KeepOutView.alpha = 0;
[self.view addSubview:_KeepOutView];
//下拉列表
_dropDownMenuView = [[testView alloc] init];
_dropDownMenuView.userInteractionEnabled = YES;
_dropDownMenuView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_dropDownMenuView];
_dropDownMenuView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 105);
_dropDownMenuView.alpha = 0;
_dropDown = NO;
__weak typeof(self) weakSelf = self;
_dropDownMenuView.searchTypeVCBlock = ^(NSInteger searchInteger){
_typeInteger = searchInteger;
weakSelf.dropDownMenuView.alpha = 0;
weakSelf.KeepOutView.alpha = 0;
[weakSelf loadData];
[weakSelf createUI];
[weakSelf doaction];
};
(3)、调用下拉列表
//搜索类型选择
_typeLabel = [[UILabel alloc] init];
_typeLabel.text = _typeArray[_typeInteger];
_typeLabel.font = GetFont(BFONT_12);
_typeLabel.textAlignment = 1;
_typeLabel.layer.borderWidth = 1;
_typeLabel.layer.borderColor = UIColorFromRGBOne(0x99).CGColor;
_typeLabel.textColor = UIColorFromRGBOne(0x73);
[searchView addSubview:_typeLabel];
[_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(32, 26));
make.leftMargin.equalTo(searchView).with.offset(-1);
make.topMargin.equalTo(searchView).with.offset(-1);
}];
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLabel)];
_typeLabel.userInteractionEnabled = YES;
[_typeLabel addGestureRecognizer:tapGesture];
(4)、响应方法,设施知否显示下拉列表
#pragma mark - 分类选择商品
-(void)clickLabel{
_dropDown = !_dropDown;
if (_dropDown) {
_dropDownMenuView.alpha = 1;
_KeepOutView.alpha = 0.3;
}else{
_dropDownMenuView.alpha = 0;
_KeepOutView.alpha = 0;
}
}
点击其他方法取消下拉列表的方法就很简单就不在这里具体说了。需要注意的是,下拉列表的View,和遮挡视图的View添加顺序,先添加遮挡视图。
希望对你能有帮助