ICRCheckBox.m 2.2 KB
Newer Older
mei's avatar
mei committed
1 2
//
//  ICRCheckBox.m
mei's avatar
mei committed
3
//  XFFruit
mei's avatar
mei committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//
//  Created by Xummer on 15/3/30.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ICRCheckBox.h"

@interface ICRCheckBox ()
@property (strong, nonatomic) UIButton *m_checkBoxBtn;

@property (strong, nonatomic) UIImageView *m_checkBoxBG;
@end

@implementation ICRCheckBox

#pragma mark - Life Cycle
mei's avatar
mei committed
20

mei's avatar
mei committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }
    
    [self initSubviews];
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _m_checkBoxBG.origin = (CGPoint){
        .x = (self.width - _m_checkBoxBG.width) * .5f,
        .y = (self.height - _m_checkBoxBG.height) * .5f
    };
    
    _m_checkBoxBtn.frame = self.bounds;
}

#pragma mark - Setter
- (void)setIsSelected:(BOOL)isSelected {
    if (_isSelected == isSelected) {
        return;
    }
    
    _isSelected = isSelected;
    _m_checkBoxBtn.selected = _isSelected;
}

mei's avatar
mei committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
- (void)setM_eBGType:(CheckBoxBGType)eBGType {
    if (_m_eBGType == eBGType) {
        return;
    }
    _m_eBGType = eBGType;
    
    NSString *nsBGIcon = nil;
    switch (_m_eBGType) {
        case kCheckBoxBGWhite:
            nsBGIcon = @"LoginCheckBox";
            break;
        case kCheckBoxBGGray:
            nsBGIcon = @"GrayCheckBox";
            break;
            
        default:
            break;
    }
    
    self.m_checkBoxBG.image = [UIImage imageNamed:nsBGIcon];
    
}

mei's avatar
mei committed
76 77 78 79
#pragma mark - Private Method
- (void)initSubviews {
    self.m_checkBoxBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"LoginCheckBox"]];
    [self addSubview:_m_checkBoxBG];
mei's avatar
mei committed
80
    self.m_eBGType = kCheckBoxBGWhite;
mei's avatar
mei committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    
    self.m_checkBoxBtn = [IBTUIButton buttonWithType:UIButtonTypeCustom];
    [self.m_checkBoxBtn setImage:[UIImage imageNamed:@"LoginCheckMark"]
                        forState:UIControlStateSelected];
    [self.m_checkBoxBtn addTarget:self
                           action:@selector(onCheckBoxAction:)
                 forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_m_checkBoxBtn];
}

#pragma mark - Actions
- (void)onCheckBoxAction:(id)sender {
    self.isSelected = !_isSelected;
}

@end