1、什么是控件:
凡是UIController类以及子类的对象才叫控件
控件会触发事件
**2、UIButton **
UIButton 是 UIControl的子类
UIControl 是 UIView的子类
2.1 作用:与用户交互,用户可以点击,并提供响应
2.2 核心属性:
title: 按键上的文字
image: 按键上的图片
backgroundImage: 背景图片
state:状态
tag 值用来辨别button
frame: 大小
2.3 高级事件:
touchUpInside 事件 在button区域内抬起
核心属性:
UIButtonTypeCustom 自定义样式
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIButtonTypeSystem 系统样式
UIButtonTypeDetailDisclosure, 圆圈i
UIButtonTypeInfoLight, 圆圈i
UIButtonTypeInfoDark, 圆圈i
UIButtonTypeContactAdd, 圆圈+
UIButton大小:
button.frame = CGRectMake(100, 100, 100, 60);
UIButton 状态:
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) // Applicable only when the screen supports focus
UIControlStateApplication // additional flags available for application use
UIControlStateReserved // flags reserved for internal framework use
UIButton 上文字以及文字颜色:
[button setTitle:@"正常" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
UIButton 背景图片:
button.backgroundColor = [UIColor purpleColor];
UIButton 按键上的图片:
[button setImage:[UIImage imageNamed:@"abc"] forState:UIControlStateNormal]; //正常时的背景图片
注意:/*选中状态 和 失效状态必须通过代码修改*/
[button setBackgroundImage:[UIImage imageNamed:@"BTN2"] forState:UIControlStateSelected]; //选中时的背景图片
[button setBackgroundImage:[UIImage imageNamed:@"disable"] forState:UIControlStateDisabled]; //失效状态时的背景图片
[button setImage:[UIImage imageNamed:@"stop"] forState:UIControlStateHighlighted];//鼠标移动到按钮上
tag 在父视图中的一个标示
button.tag = 100;
让button失效
button.enabled = NO;
高级事件:UIControlEvents
UIControlEventTouchDown = 1 << 0, // on all touch downs
UIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1)
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,//在距button顶端70像素时才会
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12, // sliders, etc.
UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semantic action: for buttons, etc.
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
UIControlEventAllTouchEvents = 0x00000FFF, // for touch events
UIControlEventAllEditingEvents = 0x000F0000, // for UITextField
UIControlEventApplicationReserved = 0x0F000000, // range available for application use
UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use
UIControlEventAllEvents = 0xFFFFFFFF
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
-(void)click:(id)sender{
NSLog(@"kwgf");
}