//
// CustomButton.m
// CustomButton
//
// Created by YouXianMing on 16/5/21.
// Copyright © 2016年 YouXianMing. All rights reserved.
//
#import "CustomButton.h"
@interface CustomButton ()
@property (nonatomic) BaseControlState state;
@property (nonatomic) BOOL enableEvent;
@property (nonatomic, strong) UILabel *normalLabel;
@property (nonatomic, strong) UILabel *highlightedLabel;
@property (nonatomic, strong) UILabel *disabledLabel;
@property (nonatomic, strong) UIView *backgroundView;
@end
@implementation CustomButton
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// 激活
_enableEvent = YES;
// 背景view
self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
self.backgroundView.backgroundColor = [UIColor clearColor];
[self addSubview:self.backgroundView];
// Label
self.normalLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.normalLabel.textAlignment = NSTextAlignmentCenter;
self.normalLabel.textColor = [UIColor clearColor];
[self addSubview:self.normalLabel];
self.highlightedLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.highlightedLabel.textAlignment = NSTextAlignmentCenter;
self.highlightedLabel.textColor = [UIColor clearColor];
[self addSubview:self.highlightedLabel];
self.disabledLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.disabledLabel.textAlignment = NSTextAlignmentCenter;
self.disabledLabel.textColor = [UIColor clearColor];
[self addSubview:self.disabledLabel];
// backgroundView
self.backgroundView.userInteractionEnabled = NO;
self.normalLabel.userInteractionEnabled = NO;
self.highlightedLabel.userInteractionEnabled = NO;
self.disabledLabel.userInteractionEnabled = NO;
}
return self;
}
- (void)setTitleColor:(UIColor *)color state:(BaseControlState)state {
if (state == BaseControlStateNormal) {
self.normalLabel.textColor = color;
} else if (state == BaseControlStateHighlighted) {
self.highlightedLabel.textColor = color;
} else if (state == BaseControlStateDisabled) {
self.disabledLabel.textColor = color;
}
}
#pragma mark - 重载的方法
- (void)touchEvent {
if (_enableEvent == NO) {
return;
}
[self changeToState:BaseControlStateNormal animated:YES];
if (self.delegate && [self.delegate respondsToSelector:@selector(baseControlTouchEvent:)]) {
[self.dele