码迷,mamicode.com
首页 > 其他好文 > 详细

ZKButton

时间:2019-09-16 09:29:57      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:lstat   display   src   上下   temp   enum   oid   stop   touch   

技术图片
//
//  ZKButton.h
//  WaterProofer
//
//  Created by HELLO WORLD on 2019/9/15.
//  Copyright © 2019年 WaterProofer. All rights reserved.
//

#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, ZKButtonType) {
    ZKButtonTypeLeftAndRight=0,//左右结构
    ZKButtonTypeTopAndBottom=0,//上下结构
};

NS_ASSUME_NONNULL_BEGIN

@interface ZKButton : UIButton
@property(nonatomic,assign)ZKButtonType type;

@property(nonatomic,strong)NSString* left_top_SelectImg;
@property(nonatomic,strong)NSString* left_top_DefaultImg;
@property(nonatomic,strong)NSString* left_top_SelectTitle;
@property(nonatomic,strong)NSString* left_top_DefaultTitle;


@property(nonatomic,strong)NSString* right_bottom_SelectImg;
@property(nonatomic,strong)NSString* right_bottom_DefaultImg;
@property(nonatomic,strong)NSString* right_bottom_SelectTitle;
@property(nonatomic,strong)NSString* right_bottom_DefaultTitle;

@property(nonatomic,assign)BOOL isSelect;

@property(nonatomic,strong)UIButton* left_top;//左上
@property(nonatomic,strong)UIButton* right_bottom;//右下

@property(nonatomic,copy)void(^selectBlock)(BOOL isSelect,NSInteger selectIndex);

@end

NS_ASSUME_NONNULL_END
ZKButton.h
技术图片
//
//  ZKButton.m
//  WaterProofer
//
//  Created by HELLO WORLD on 2019/9/15.
//  Copyright © 2019年 WaterProofer. All rights reserved.
//

#import "ZKButton.h"
#define ZKWeakSelf __weak typeof(self) weakSelf = self;


@interface ZKButton()

@end


@implementation ZKButton

-(instancetype)init{
    if (self= [super init]){
        
        _left_top = [UIButton buttonWithType:UIButtonTypeCustom];
        [_left_top addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_left_top];
        
        _right_bottom = [UIButton buttonWithType:UIButtonTypeCustom];
        [_right_bottom addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _right_bottom.userInteractionEnabled = YES;
        _right_bottom.imageView.userInteractionEnabled = YES;
        [self addSubview:_right_bottom];
    }
    return self;
}
-(void)setType:(ZKButtonType)type{
    _type = type;
    ZKWeakSelf
    switch (_type) {
        case 0:
        {//左右结构
            [_left_top mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self);
                make.left.equalTo(self.mas_left).offset(5);
            }];
            [_right_bottom mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(self);
                make.left.equalTo(weakSelf.left_top.mas_right).with.offset(5);
                make.right.equalTo(self.mas_right).offset(-5);
            }];
        }
            break;
        case 1:
        {//上下结构
            [_left_top mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.top.equalTo(self.mas_top).offset(5);
            }];
            [_right_bottom mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self);
                make.top.equalTo(weakSelf.left_top.mas_bottom).with.offset(5);
                make.bottom.equalTo(self.mas_bottom).offset(-5);
            }];
        }
            break;
            
        default:
            break;
    }
}

#pragma - mark - left_top
-(void)setLeft_top_DefaultImg:(NSString *)left_top_DefaultImg{
    _left_top_DefaultImg = left_top_DefaultImg;
    [_left_top setImage:[UIImage imageNamed:_left_top_DefaultImg] forState:UIControlStateNormal];
}
-(void)setLeft_top_SelectImg:(NSString *)left_top_SelectImg{
    _left_top_SelectImg = left_top_SelectImg;
    [_left_top setImage:[UIImage imageNamed:_left_top_SelectImg] forState:UIControlStateSelected];
}

-(void)setLeft_top_DefaultTitle:(NSString *)left_top_DefaultTitle{
    _left_top_DefaultTitle = left_top_DefaultTitle;
    [_left_top setTitleColor:ColorHex_666666 forState:UIControlStateNormal];
    [_left_top setTitle:_left_top_DefaultTitle forState:UIControlStateNormal];
}
-(void)setLeft_top_SelectTitle:(NSString *)left_top_SelectTitle{
    _left_top_SelectTitle = left_top_SelectTitle;
    [_left_top setTitleColor:ColorHex_666666 forState:UIControlStateSelected];
    [_left_top setTitle:_left_top_SelectTitle forState:UIControlStateSelected];
}

#pragma - mark - right_bottom
-(void)setRight_bottom_DefaultImg:(NSString *)right_bottom_DefaultImg{
    _right_bottom_DefaultImg = right_bottom_DefaultImg;
    [_right_bottom setImage:[UIImage imageNamed:_right_bottom_DefaultImg] forState:UIControlStateNormal];
}
-(void)setRight_bottom_SelectImg:(NSString *)right_bottom_SelectImg{
    _right_bottom_SelectImg = right_bottom_SelectImg;
    [_right_bottom setImage:[UIImage imageNamed:_right_bottom_SelectImg] forState:UIControlStateSelected];
}
-(void)setRight_bottom_DefaultTitle:(NSString *)right_bottom_DefaultTitle{
    _right_bottom_DefaultTitle = right_bottom_DefaultTitle;
    [_right_bottom setTitleColor:ColorHex_666666 forState:UIControlStateNormal];
    [_right_bottom setTitle:_right_bottom_DefaultTitle forState:UIControlStateNormal];
}
-(void)setRight_bottom_SelectTitle:(NSString *)right_bottom_SelectTitle{
    _right_bottom_SelectTitle = right_bottom_SelectTitle;
    [_right_bottom setTitleColor:ColorHex_666666 forState:UIControlStateSelected];
    [_right_bottom setTitle:_right_bottom_SelectTitle forState:UIControlStateSelected];
}

-(void)setIsSelect:(BOOL)isSelect{
    _isSelect = isSelect;
    _left_top.selected = _isSelect;
    _right_bottom.selected = _isSelect;
}
-(void)btnClick:(UIButton*)btn{
    //self.isSelect = !self.isSelect;//后期扩展功能用适合多选择 
    if (self.selectBlock){
        self.selectBlock(YES, self.tag);
    }
}
@end
ZKButton.m
技术图片
-(void)createSelectButton
{
    _buttonArray = [NSMutableArray array];
    ZKButton* tempBtn = nil;
    WPWeakSelf
    for(int i=0 ; i<4; i++){
        ZKButton* button = [[ZKButton alloc] init];
        button.selectBlock = ^(BOOL isSelect, NSInteger selectIndex) {
            [weakSelf.buttonArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                ZKButton *button = (ZKButton *)obj;
                button.isSelect = (button.tag==selectIndex)?YES:NO;
            }];
        };
        //button.hidden = YES;
        button.tag = i+100;
        button.left_top_DefaultImg = @"hire_line_default";
        button.left_top_SelectImg=@"hire_line_select";
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.type = ZKButtonTypeLeftAndRight;
        [self.btnView addSubview:button];
        if (i==0) {
            button.isSelect = YES;
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_btnView.mas_left).offset(10);
                //make.size.mas_equalTo(CGSizeMake(20, 20));
                make.centerY.equalTo(_btnView.mas_centerY);
            }];
        }else{
            button.isSelect = NO;
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(tempBtn.mas_right).offset(10);
                //make.size.mas_equalTo(CGSizeMake(20, 20));
                make.centerY.equalTo(_btnView.mas_centerY);
            }];
        }
        
        tempBtn = button;
        [_buttonArray addObject:button];
    }
}



-(void)buttonClick:(ZKButton *)sender
{
    [_buttonArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        ZKButton *button = (ZKButton *)obj;
        button.isSelect = NO;
    }];

    sender.isSelect = YES;
}
createSelectButton

 

ZKButton

标签:lstat   display   src   上下   temp   enum   oid   stop   touch   

原文地址:https://www.cnblogs.com/kingstudy/p/11525366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!