iOS筛选器

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容

  • ## 可重入函数 ### 可重入性的理解 若一个程序或子程序可以安全的被并行执行,则称其为可重入的;即当该子程序正...
    夏至亦韵阅读 698评论 0 0
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,679评论 0 9
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,124评论 29 470
  • 写在前面的话:收到了简书上小伙伴的简信留言,心里非常的激动,感叹于我的文字被你们关注的同时,更感恩简书网络文字间的...
    Danica丹妮卡阅读 443评论 2 1
  • Linux操作-安装JDK 本次安装的是JDK1.7,32位的操作系统可以使用"jdk-7u65-linux-i5...
    李程鹏阅读 231评论 0 0