代码改变世界

提示选择框

2016-01-18 12:49  000aaa  阅读(192)  评论(0编辑  收藏  举报
#import <UIKit/UIKit.h>

@interface MgsSelectListView : UIView

@property(nonatomic,strong)NSArray *allkeys;
@property(nonatomic,strong)NSDictionary *datadictionary;
@property(nonatomic,strong)void (^callback)(BOOL success,NSString *keyString,NSString *showString);


/*
 param1 : 父view  
 param2 : 展示数据的格式 @{@"key1":@"value1",@"key2":@"value"}
 param3 : 筛选类型的标题
 callback : 选中数据的 key value (比如: key1,value1)
 */
+(void)showInSuperView:(UIView *)superView dataDic:(NSDictionary *)aDictionary title:(NSString *)titleText callback:(void(^)(BOOL success,NSString *keyString,NSString *showString))callback;
@end
//
//  MgsSelectListView.m
//  提示筛选
//
//  Created by admin on 16/1/15.
//  Copyright © 2016年 123. All rights reserved.
//

#import "MgsSelectListView.h"

@implementation MgsSelectListView

+(void)showInSuperView:(UIView *)superView dataDic:(NSDictionary *)aDictionary title:(NSString *)titleText callback:(void (^)(BOOL, NSString *keyString, NSString *showString))callback
{
    MgsSelectListView *mgs = (MgsSelectListView *)[superView viewWithTag:9326];
    if (!mgs)
    {
        mgs = [MgsSelectListView new];
        mgs.tag = 9326;
        [superView addSubview:mgs];
    }
    
    
    UIButton *background = (UIButton *)[superView viewWithTag:8359];
    if (!background)
    {
        background = [[UIButton alloc]initWithFrame:superView.frame];
        background.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        background.tag = 8359;
        [background addTarget:mgs action:@selector(backgroundClick:) forControlEvents:UIControlEventTouchUpInside];
        [superView addSubview:background];
    }
    
    UIView *dataView = [superView viewWithTag:62495];
    if (!dataView)
    {
        dataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, superView.bounds.size.width - 100, (aDictionary.count+1)*44+15)];
        dataView.center = superView.center;
        dataView.tag = 62495;
        dataView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        dataView.layer.cornerRadius = 12.0f;
        dataView.clipsToBounds = YES;
        dataView.layer.masksToBounds = YES;
        [superView addSubview:dataView];
        
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,dataView.bounds.size.width, 44)];
        titleLabel.text = titleText;
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [dataView addSubview:titleLabel];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(1, 44, dataView.bounds.size.width-2,1)];
        imageView.image = [UIImage imageNamed:@"halving"];
        [dataView addSubview:imageView];
        
        
        mgs.allkeys = [aDictionary allKeys];
        mgs.datadictionary = aDictionary;
        for (int i = 0;i < mgs.allkeys.count;i++)
        {
            UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [inButton setTitle:aDictionary[mgs.allkeys[i]] forState:UIControlStateNormal];
            //怎么实现让button中的文字左对齐  或者右对齐
            inButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            //设置button的标题离边界的距离
            inButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
            [inButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            inButton.backgroundColor = [UIColor clearColor];
            inButton.tag = 99000+i;
            inButton.frame = CGRectMake(0, 49 + i*44, dataView.bounds.size.width, 44);
            [inButton addTarget:mgs action:@selector(inButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [dataView addSubview:inButton];
        }
    }
    [UIView animateWithDuration:0.5 animations:^{
        [superView bringSubviewToFront:background];
        [superView bringSubviewToFront:dataView];
        dataView.hidden = NO;
        background.hidden = NO;
    }];
    mgs.callback = callback;
}
-(void)backgroundClick:(UIButton *)sender
{
    UIView *superView = sender.superview;
    UIView *dataView = [superView viewWithTag:62495];
    [UIView animateWithDuration:0.5 animations:^{
        [superView sendSubviewToBack:sender];
        [superView sendSubviewToBack:dataView];
        dataView.hidden = YES;
        sender.hidden = YES;
    }];
}
-(void)inButtonClick:(UIButton *)sender
{
    NSInteger index = sender.tag - 99000;
    NSString *key = self.allkeys[index];
    NSString *value = self.datadictionary[key];
    self.callback(YES,key,value);
    
    UIView *dataView = sender.superview;
    UIView *superView = dataView.superview;
    UIButton *background = (UIButton *)[superView viewWithTag:8359];
    [UIView animateWithDuration:0.5 animations:^{
        [superView sendSubviewToBack:background];
        [superView sendSubviewToBack:dataView];
        dataView.hidden = YES;
        background.hidden = YES;
    }];
}
@end

 

调用方式:

1.导入头文件

#import "MgsSelectListView.h"

2.调用封装的类方法(按指定得格式传入数据)

- (IBAction)buttonClick:(id)sender
{
    [MgsSelectListView showInSuperView:self.view dataDic:@{@"yl":@"银联支付",@"zfb":@"支付宝支付",@"qq":@"QQ支付",@"wx":@"微信支付"} title:@"请选择支付类型" callback:^(BOOL success, NSString *keyString, NSString *showString) {
        _label.text = showString;
    }];
}

 

3.在回调部分可以得到选中的信息,做相应的处理操作!!