在介绍UIButton的用法前,要先了解一下它的父类UIControl,UIControl是所有具有事件处理功能控件的父类。
而该类控件一般响应事件又有三种形式:基于触摸、基础值、基础编辑。控件的层次关系图如下:
UIControl类的常用方法:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; //添加一个事件
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; //删除一个事件
它的子类因为继承了这些方法,所以也经常用到,比如UIButton。
以上两个方法的中最后一个参数,UIControlEvents为点击事件,是枚举类型。
UIControlEventTouchDown // 用户按下时触发
UIControlEventTouchDownRepeat// 点击次数大于1时触发
UIControlEventTouchDragInside// 当触摸在控件内拖动时触发
UIControlEventTouchDragOutside// 当触摸在控件之外拖动时触发
UIControlEventTouchDragEnter// 当触摸从控件外拖动到内部时
UIControlEventTouchDragExit// 当触摸从控件内拖动到外部时
UIControlEventTouchUpInside// 在控件内触摸抬起时
UIControlEventTouchUpOutside// 在控件外触摸抬起时
UIControlEventTouchCancel// 触摸取消事件,设备被锁上或者电话呼叫打断
UIControlEventValueChanged// 当控件的值发生改变时
UIControlEventEditingDidBegin//文本控件开始编辑时
UIControlEventEditingChanged//文本控件的文本改变
UIControlEventEditingDidEnd//文本控件结束编辑时
UIControlEventEditingDidEndOnExit// 文本控件内通过按下回车键结束编辑时
UIControlEventAllTouchEvents// 所有触摸事件
UIControlEventAllEditingEvents// 文本编辑的所有事件,for UITextField
UIControlEventApplicationReserved// range available for application use
UIControlEventSystemReserved// range reserved for internal framework use
UIControlEventAllEvents//所有事件
UIButton
常用方法如下:
- (void)setTitle:(NSString *)title forState:(UIControlState)state; // default is nil. title is assumed to be single line
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state; // default if nil. use opaque white
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state; // default is nil. use 50% black
- (void)setImage:(UIImage *)image forState:(UIControlState)state; // default is nil. should be same size if different for different states
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state; // default is nil
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; //添加一个事件
UIControlState也是个枚举类型:
UIControlStateNormal //正常状态
UIControlStateHighlighted// 高亮状态
UIControlStateDisabled//禁用状态
UIControlStateSelected// 选中状态
UIControlStateApplication// additional flags available for application use
UIControlStateReserved
注意:上面的方法中,setTitle和setImage方法不能同时使用,同时使用,只会显示图片,不显示标题。
如果要设置图片和标题,应该setBackgroundImage和setTitle或者自定义一个UIButton的子类来实现。
UIButton各属性设置的实例代码:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// 设置frame,与按钮的类型有关
button.frame = CGRectMake(90, 100, 41, 30);
// 禁用按钮
button.enabled = NO;
// 按钮选中
button.selected = YES;
// 设置标题,状态正常
[button setTitle:@"normal" forState:UIControlStateNormal];
// 设置标题,状态高亮
[button setTitle:@"highlighted" forState:UIControlStateHighlighted];
// 设置标题,状态禁用
[button setTitle:@"disabled" forState:UIControlStateDisabled];
// 设置标题,状态选中
[button setTitle:@"selected" forState:UIControlStateSelected];
// 设置title的颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//设置标题的字体
button.titleLabel.font = [UIFont systemFontOfSize:14.0f];
// 设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"love_select"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:@"love_select"] forState:UIControlStateSelected];
// 设置图片
[button setImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"love_select"] forState:UIControlStateSelected];
// 用户在控件内按下抬起时
[button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
// 用户按下时
[button addTarget:self action:@selector(down) forControlEvents:UIControlEventTouchDown];
// 用户按下时
[button addTarget:self action:@selector(outside) forControlEvents:UIControlEventTouchUpOutside];
// 记录用户多次按下
[button addTarget:self action:@selector(repeat) forControlEvents:UIControlEventTouchDownRepeat];
// 用户由内向外
[button addTarget:self action:@selector(dragExit) forControlEvents:UIControlEventTouchDragExit];
// 用户由外向内
[button addTarget:self action:@selector(dragEnter) forControlEvents:UIControlEventTouchDragEnter];
// 事件的取消
[button addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchCancel];
注意了,如果想要UIButton的背景图片显示一张网络图片的话,可以用SDWebImage这个开源框架。
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u011619283/article/details/23447903