button状态
Posted liuzhenjie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了button状态相关的知识,希望对你有一定的参考价值。
/*
一、按钮的状态
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
以上是关于button状态的主要内容,如果未能解决你的问题,请参考以下文章