美文网首页
iOS筛选器

iOS筛选器

作者: 怪客半 | 来源:发表于2017-03-25 10:02 被阅读315次
    Filter.gif

    调用:

        QHBillFilter *filter = [[QHBillFilter alloc] initWithBillType:BillType_Payment];
        CGRect rect = filter.frame;
        rect.origin.y = 300;
        filter.frame = rect;
        [self.view addSubview:filter];
    

    实现
    .h

    #import <UIKit/UIKit.h>
    
    typedef enum : NSUInteger {
        FilterType_Union,
        FilterType_Alipay,
        FilterType_Wechat,
        FilterType_Success,
        FilterType_Deal,
        FilterType_Fail
    } FilterType;
    
    typedef enum : NSUInteger {
        BillType_Payment,
        BillType_Encash
    } BillType;
    
    #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
    #define RGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
    #define kColor1                         RGBA(71, 71, 71, 1.0)
    #define kColor3                         RGBA(119, 119, 119, 1.0)
    #define kColor4                         RGBA(241, 241, 241, 1.0)
    
    @interface FilterButton : UIButton
    
    @property (nonatomic) FilterType type;
    @property (nonatomic, strong) UIImageView *logo;
    @property (nonatomic, strong) UILabel *label;
    
    + (instancetype)buttonWithType:(UIButtonType)buttonType frame:(CGRect)frame filterType:(FilterType)type;
    
    @end
    
    @interface QHBillFilter : UIView
    
    @property (nonatomic) BillType type;
    @property (nonatomic, strong) UIButton *selectTimeButton;
    @property (nonatomic, strong) UIButton *confirmButton;
    @property (nonatomic, strong) UILabel *beginLabel;
    @property (nonatomic, strong) UILabel *endLabel;
    
    - (instancetype)initWithBillType:(BillType)type;
    
    @end
    
    
    

    .m

    #import "QHBillFilter.h"
    
    @implementation FilterButton
    
    + (instancetype)buttonWithType:(UIButtonType)buttonType frame:(CGRect)frame filterType:(FilterType)type {
        FilterButton *button = [super buttonWithType:buttonType];
        button.type = type;
        [button setFrame:frame];
        [button setBackgroundColor:[UIColor clearColor]];
        [button initWithSubviews];
        return button;
    }
    
    - (void)initWithSubviews {
        CGFloat logoW = 0.18*self.frame.size.width;
        CGPoint logoCenter = CGPointMake(0.25*self.frame.size.width, self.frame.size.height/2);
        
        self.logo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, logoW, logoW)];
        [self.logo setCenter:logoCenter];
        [self addSubview:self.logo];
        
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.logo.frame), 0, self.frame.size.width - CGRectGetMaxX(self.logo.frame), self.frame.size.height)];
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.font = [UIFont systemFontOfSize:18.0f];
        self.label.textColor = [UIColor blackColor];
        [self addSubview:self.label];
        
        NSString *imageName,*title;
        
        if (self.type == FilterType_Union) {
            imageName = @"bill_union_y.png";
            title = @"貔貅";
        }
        else if (self.type == FilterType_Alipay) {
            imageName = @"bill_alipay_y.png";
            title = @"角盥漱";
        }
        else if (self.type == FilterType_Wechat) {
            imageName = @"bill_wechat_y.png";
            title = @"猫又";
        }
        else if (self.type == FilterType_Success) {
            imageName = @"bill_success_y.png";
            title = @"成功";
        }
        else if (self.type == FilterType_Deal) {
            imageName = @"bill_init_y.png";
            title = @"处理中";
        }
        else if (self.type == FilterType_Fail) {
            imageName = @"bill_fail_y.png";
            title = @"失败";
        }
        
        self.logo.image = [UIImage imageNamed:imageName];
        self.label.text = title;
    }
    
    @end
    
    @interface QHBillFilter()
    
    @property (nonatomic, assign) CGFloat rowH1;
    @property (nonatomic, assign) CGFloat rowH2;
    @property (nonatomic, assign) CGFloat lineY;
    @property (nonatomic, strong) UIView *payWayFilter;
    @property (nonatomic, strong) UIView *stateFilter;
    
    @end
    
    @implementation QHBillFilter
    
    - (instancetype)initWithBillType:(BillType)type {
        self = [super init];
        self.type = type;
        if (self) {
            [self createFilter];
        }
        return self;
    }
    
    - (void)createFilter {
        _rowH1 = SCREEN_WIDTH*150/1122;//效果图比例
        _rowH2 = SCREEN_WIDTH*160/1122;
        _lineY = 20;
        
        if (self.type == BillType_Encash) {
            self.frame = CGRectMake(0, 60, SCREEN_WIDTH, _rowH1*2 + _rowH2);
            [self createTimeFilter];
            self.stateFilter = [self createTypeFilterIsPayWay:NO];
            [self.stateFilter setFrame:CGRectMake(0, _rowH1, SCREEN_WIDTH, _rowH2)];
            [self addSubview:self.stateFilter];
        }
        else if (self.type == BillType_Payment) {
            self.frame = CGRectMake(0, 60, SCREEN_WIDTH, _rowH1*2 + _rowH2*2);
            [self createTimeFilter];
            self.payWayFilter = [self createTypeFilterIsPayWay:YES];
            [self.payWayFilter setFrame:CGRectMake(0, _rowH1, SCREEN_WIDTH, _rowH2)];
            [self addSubview:self.payWayFilter];
            
            self.stateFilter = [self createTypeFilterIsPayWay:NO];
            [self.stateFilter setFrame:CGRectMake(0, _rowH1+_rowH2, SCREEN_WIDTH, _rowH2)];
            [self addSubview:self.stateFilter];
        }
        
        CGFloat buttonY = CGRectGetMaxY(self.stateFilter.frame);
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton setFrame:CGRectMake(0, buttonY, SCREEN_WIDTH/2, _rowH1)];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:kColor3 forState:UIControlStateNormal];
        [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:18.0f]];
        [cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cancelButton];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, buttonY, 1, _rowH1)];
        line.backgroundColor = kColor4;
        [self addSubview:line];
        
        self.confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.confirmButton setFrame:CGRectMake(SCREEN_WIDTH/2, buttonY, SCREEN_WIDTH/2, _rowH1)];
        [self.confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [self.confirmButton setTitleColor:RGBA(44, 172, 253, 1.0) forState:UIControlStateNormal];
        [self.confirmButton.titleLabel setFont:[UIFont systemFontOfSize:18.0f]];
        [self addSubview:self.confirmButton];
        
        self.backgroundColor = [UIColor whiteColor];
    }
    
    - (void)cancel {
        NSLog(@"取消");
        [self.stateFilter.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[FilterButton class]]) {
                FilterButton *button = (FilterButton *)obj;
                button.selected = YES;
                [self changeState:button];
            }
        }];
        [self.payWayFilter.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[FilterButton class]]) {
                FilterButton *button = (FilterButton *)obj;
                button.selected = YES;
                [self changeState:button];
            }
        }];
    }
    
    - (void)confirm {
        NSLog(@"确定");
    }
    
    - (void)createTimeFilter {
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH/8, _rowH1)];
        label1.text = @"时间";
        label1.textColor = kColor3;
        label1.font = [UIFont systemFontOfSize:16.0f];
        [self addSubview:label1];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label1.frame)+5, _lineY, 1, _rowH1-2*_lineY)];
        line.backgroundColor = kColor4;
        [self addSubview:line];
        
        self.beginLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(line.frame), 0, SCREEN_WIDTH/4+20, _rowH1)];
        self.beginLabel.text = @"2017-03-18";
        self.beginLabel.textColor = kColor1;
        self.beginLabel.font = [UIFont systemFontOfSize:18.0f];
        self.beginLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.beginLabel];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.beginLabel.frame), 0, 40, _rowH1)];
        label2.text = @"至";
        label2.textColor = kColor3;
        label2.font = [UIFont systemFontOfSize:16.0f];
        label2.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label2];
    
        self.endLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(label2.frame), 0, SCREEN_WIDTH/4+20, _rowH1)];
        self.endLabel.text = @"2017-03-18";
        self.endLabel.textColor = kColor1;
        self.endLabel.font = [UIFont systemFontOfSize:18.0f];
        self.endLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.endLabel];
        
        self.selectTimeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.selectTimeButton setFrame:CGRectMake(CGRectGetMaxX(label1.frame), 0, SCREEN_WIDTH-CGRectGetMaxX(label1.frame), _rowH1)];
        [self addSubview:self.selectTimeButton];
        
        UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, _rowH1-1, SCREEN_WIDTH, 1)];
        line2.backgroundColor = kColor4;
        [self addSubview:line2];
    }
    
    - (UIView *)createTypeFilterIsPayWay:(BOOL)isValue {
        //支付
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _rowH2)];
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH/4 - 20, _rowH2)];
        label1.text = @"状态(单选)";
        if (isValue) {
            label1.text = @"支付(单选)";
        }
        label1.textColor = kColor3;
        label1.font = [UIFont systemFontOfSize:13.0f];
        [view addSubview:label1];
        
        UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4, _lineY, 1, _rowH2 - 2*_lineY)];
        line1.backgroundColor = kColor4;
        [view addSubview:line1];
        
        FilterType type1,type2,type3;
        
        if (isValue) {
            type1 = FilterType_Union;
            type2 = FilterType_Alipay;
            type3 = FilterType_Wechat;
        }
        else {
            type1 = FilterType_Success;
            type2 = FilterType_Deal;
            type3 = FilterType_Fail;
        }
        
        FilterButton *button1 = [FilterButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(SCREEN_WIDTH/4, 0, SCREEN_WIDTH/4, _rowH2) filterType:type1];
        [button1 addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventTouchUpInside];
        button1.selected = YES;
        [view addSubview:button1];
        
        UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2, _lineY, 1, _rowH2 - 2*_lineY)];
        line2.backgroundColor = kColor4;
        [view addSubview:line2];
        
        FilterButton *button2 = [FilterButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/4, _rowH2) filterType:type2];
        [button2 addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventTouchUpInside];
        button2.selected = YES;
        [view addSubview:button2];
        
        UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4*3, _lineY, 1, _rowH2 - 2*_lineY)];
        line3.backgroundColor = kColor4;
        [view addSubview:line3];
        
        FilterButton *button3 = [FilterButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(SCREEN_WIDTH/4*3, 0, SCREEN_WIDTH/4, _rowH2) filterType:type3];
        [button3 addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventTouchUpInside];
        button3.selected = YES;
        [view addSubview:button3];
        
        UIView *line4 = [[UIView alloc] initWithFrame:CGRectMake(0, _rowH2-1, SCREEN_WIDTH, 1)];
        line4.backgroundColor = kColor4;
        [view addSubview:line4];
        
        return view;
    }
    
    - (void)changeState:(FilterButton *)button {
        NSString *imageName,*title;
        if (button.selected) {
            if (button.type == FilterType_Union) {
                imageName = @"bill_union_n.png";
                title = @"貔貅";
            }
            else if (button.type == FilterType_Alipay) {
                imageName = @"bill_alipay_n.png";
                title = @"角盥漱";
            }
            else if (button.type == FilterType_Wechat) {
                imageName = @"bill_wechat_n.png";
                title = @"猫又";
            }
            else if (button.type == FilterType_Success) {
                imageName = @"bill_success_n.png";
                title = @"成功";
            }
            else if (button.type == FilterType_Deal) {
                imageName = @"bill_init_n.png";
                title = @"处理中";
            }
            else if (button.type == FilterType_Fail) {
                imageName = @"bill_fail_n.png";
                title = @"失败";
            }
        }
        else {
            if (button.type == FilterType_Union) {
                imageName = @"bill_union_y.png";
                title = @"貔貅";
            }
            else if (button.type == FilterType_Alipay) {
                imageName = @"bill_alipay_y.png";
                title = @"角盥漱";
            }
            else if (button.type == FilterType_Wechat) {
                imageName = @"bill_wechat_y.png";
                title = @"猫又";
            }
            else if (button.type == FilterType_Success) {
                imageName = @"bill_success_y.png";
                title = @"成功";
            }
            else if (button.type == FilterType_Deal) {
                imageName = @"bill_init_y.png";
                title = @"处理中";
            }
            else if (button.type == FilterType_Fail) {
                imageName = @"bill_fail_y.png";
                title = @"失败";
            }
        }
        button.logo.image = [UIImage imageNamed:imageName];
        button.label.text = title;
        button.selected = !button.selected;
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS筛选器

          本文链接:https://www.haomeiwen.com/subject/abhqottx.html