标签:
/*
一、按钮的状态
1.UIControlStateNormal
1> 除开UIControlStateHighlighted、UIControlStateDisabled、UIControlStateSelected以外的其他情况,都是normal状态
2> 这种状态下的按钮【可以】接收点击事件
2.UIControlStateHighlighted
1> 【当按住按钮不松开】或者【highlighted = YES】时就能达到这种状态
2> 这种状态下的按钮【可以】接收点击事件
3.UIControlStateDisabled
1> 【button.enabled = NO】时就能达到这种状态
2> 这种状态下的按钮【无法】接收点击事件
4.UIControlStateSelected
1> 【button.selected = YES】时就能达到这种状态
2> 这种状态下的按钮【可以】接收点击事件
二、让按钮无法点击的2种方法
1> button.enabled = NO;
*【会】进入UIControlStateDisabled状态
2> button.userInteractionEnabled = NO;
*【不会】进入UIControlStateDisabled状态,继续保持当前状态
*/
#import "ViewController.h"
#import "LZJButton.h"
@interface ViewController ()
/** 按钮 */
@property (nonatomic, weak) LZJButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
LZJButton *button = [[LZJButton alloc] init];
[button setTitle:@"我是按钮" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
button.frame = CGRectMake(100, 100, 100, 30);
[button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
self.button = button;
}
- (void)buttonClick {
NSLog(@"%s", __func__);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
self.button.selected = YES;
// self.button.highlighted = YES;
}
---------------------------------------
#import "LZJButton.h"
@implementation LZJButton
- (BOOL)isHighlighted
{
return YES;
}
//- (void)setHighlighted:(BOOL)highlighted
//{
//
//}
@end
标签:
原文地址:http://www.cnblogs.com/liuzhenjie/p/5296095.html