首先,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,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
---------------------------------------------------------------------------------------------------UIControlEventValueChanged = 1 << 12, // sliders, 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
http://blog.163.com/cz_jdton/blog/static/92732504201282543017312/
在这个网友的日志里看到一些内容,大体如下:
UIControlEventTouchDown
单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat
多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside
当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside
当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter
当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit
当一次触摸从控件窗口内部拖动到外部时。
UIControlEventTouchUpInside
所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside
所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel
所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged
当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin
当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged
当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd
当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit
当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents
通知所有触摸事件。
UIControlEventAllEditingEvents
通知所有关于文本编辑的事件。
UIControlEventAllEvents
通知所有事件。
为了验证下,做了一个实验:
009 | #import "ViewController.h" |
011 | @interface ViewController () |
015 | @implementation ViewController |
020 | UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
021 | [button setFrame:CGRectMake(10, 100, 300, 40)]; |
022 | [button setTitle:@ "Test" forState:UIControlStateNormal]; |
023 | [button addTarget:self action:@selector(TouchDown:) forControlEvents:UIControlEventTouchDown]; |
024 | [button addTarget:self action:@selector(TouchDownRepeat:) forControlEvents:UIControlEventTouchDownRepeat]; |
025 | [button addTarget:self action:@selector(TouchDragInside:) forControlEvents:UIControlEventTouchDragInside]; |
026 | [button addTarget:self action:@selector(TouchDragOutside:) forControlEvents:UIControlEventTouchDragOutside]; |
027 | [button addTarget:self action:@selector(TouchDragEnter:) forControlEvents:UIControlEventTouchDragEnter]; |
028 | [button addTarget:self action:@selector(TouchDragExit:) forControlEvents:UIControlEventTouchDragExit]; |
029 | [button addTarget:self action:@selector(TouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; |
030 | [button addTarget:self action:@selector(TouchUpOutside:) forControlEvents:UIControlEventTouchUpOutside]; |
031 | [button addTarget:self action:@selector(TouchCancel:) forControlEvents:UIControlEventTouchCancel]; |
032 | UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPress:)]; |
033 | longPress.minimumPressDuration = 3; |
034 | [button addGestureRecognizer:longPress]; |
036 | [self.view addSubview:button]; |
039 | - ( void )TouchDown:(id)sender |
041 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
045 | - ( void )TouchDownRepeat:(id)sender |
047 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
050 | - ( void )TouchDragInside:(id)sender |
052 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
055 | - ( void )TouchDragOutside:(id)sender |
057 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
060 | - ( void )TouchDragEnter:(id)sender |
062 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
065 | - ( void )TouchDragExit:(id)sender |
067 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
070 | - ( void )TouchUpInside:(id)sender |
072 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
075 | - ( void )TouchUpOutside:(id)sender |
077 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
080 | - ( void )TouchCancel:(id)sender |
082 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
085 | - ( void )didReceiveMemoryWarning |
087 | [super didReceiveMemoryWarning]; |
091 | - ( void )buttonLongPress:(UILongPressGestureRecognizer *)gestureRecognizer |
093 | if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { |
094 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
118 | ------------------------------------------------------------------------------------------------------------ |
说到UIControlEvent,再谈下UIControl, UIControl继承于UIView, 可以接收事件响应,看Demo:
05 | UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(100, 100, 200, 50)]; |
06 | control.backgroundColor = [UIColor blueColor]; |
07 | [control addTarget:self action:@selector(controlViewClick_1) forControlEvents:UIControlEventTouchUpInside]; |
08 | [control addTarget:self action:@selector(controlViewClick_2) forControlEvents:UIControlEventTouchUpInside]; |
09 | [control addTarget:self action:@selector(controlViewClick_3) forControlEvents:UIControlEventTouchUpInside]; |
10 | [self.view addSubview:control]; |
14 | - ( void )controlViewClick_1 |
16 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
19 | - ( void )controlViewClick_2 |
21 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
24 | - ( void )controlViewClick_3 |
26 | NSLog(@ "%@" , NSStringFromSelector(_cmd)); |
程序打印:
2013-05-01 12:36:28.119 UIControlEventsTestDemo[1152:c07] controlViewClick_1
2013-05-01 12:36:28.122 UIControlEventsTestDemo[1152:c07] controlViewClick_2
2013-05-01 12:36:28.122 UIControlEventsTestDemo[1152:c07] controlViewClick_3