首先,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
通知所有事件。
为了验证下,做了一个实验:
001
//
002
// ViewController.m
003
// UIControlEvents
004
//
005
// Created by WeiZhen_Liu on 13-4-27.
006
// Copyright (c) 2013年 WeiZhen_Liu. All rights reserved.
007
//
008
009
import "ViewController.h"
010
011
@interface ViewController ()
012
013
@end
014
015
@implementation ViewController
016
017
- (void)viewDidLoad
018
{
019
[super viewDidLoad];
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];
035
[longPress release];
036
[self.view addSubview:button];
037
}
038
039
- (void)TouchDown:(id)sender
040
{
041
NSLog(@"%@", NSStringFromSelector(_cmd));
042
}
043
044
045
- (void)TouchDownRepeat:(id)sender
046
{
047
NSLog(@"%@", NSStringFromSelector(_cmd));
048
}
049
050
- (void)TouchDragInside:(id)sender
051
{
052
NSLog(@"%@", NSStringFromSelector(_cmd));
053
}
054
055
- (void)TouchDragOutside:(id)sender
056
{
057
NSLog(@"%@", NSStringFromSelector(_cmd));
058
}
059
060
- (void)TouchDragEnter:(id)sender
061
{
062
NSLog(@"%@", NSStringFromSelector(_cmd));
063
}
064
065
- (void)TouchDragExit:(id)sender
066
{
067
NSLog(@"%@", NSStringFromSelector(_cmd));
068
}
069
070
- (void)TouchUpInside:(id)sender
071
{
072
NSLog(@"%@", NSStringFromSelector(_cmd));
073
}
074
075
- (void)TouchUpOutside:(id)sender
076
{
077
NSLog(@"%@", NSStringFromSelector(_cmd));
078
}
079
080
- (void)TouchCancel:(id)sender
081
{
082
NSLog(@"%@", NSStringFromSelector(_cmd));
083
}
084
085
- (void)didReceiveMemoryWarning
086
{
087
[super didReceiveMemoryWarning];
088
// Dispose of any resources that can be recreated.
089
}
090
091
- (void)buttonLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
092
{
093
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
094
NSLog(@"%@", NSStringFromSelector(_cmd));
095
}
096
}
097
098
@end
099
100
/**
101
- UIControlEventTouchDown = 1 << 0, // on all touch downs
102 - UIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1)
103 - UIControlEventTouchDragInside = 1 << 2,
104 - UIControlEventTouchDragOutside = 1 << 3,
105 - UIControlEventTouchDragEnter = 1 << 4, // 注:我在模拟器上找了好久没找到这个东西什么时候触法
106 - UIControlEventTouchDragExit = 1 << 5,
107 - UIControlEventTouchUpInside = 1 << 6,
108 - UIControlEventTouchUpOutside = 1 << 7,
109 - UIControlEventTouchCancel = 1 << 8,
110 - 按下松开,依次触发: TouchDown: TouchUpInside:
111 - 按下并在按钮内部拖动最后松开,依次触发: TouchDown: TouchDragInside:(多次) TouchUpInside:
112 - 按着按钮拖动到按钮外不远处,依次触发: TouchDown: TouchDragInside:(多次) TouchUpInside:
113 - 按着按钮拖动到按钮外很远处,依次触发: TouchDown: TouchDragInside:(多次) TouchDragExit: TouchDragOutside:(多次) TouchUpOutside:
114 - 快速双击两下,依次触发: TouchDown: TouchUpInside: TouchDown: TouchDownRepeat: TouchUpInside:
115 - 快速击三下,依次触发: TouchDown: TouchUpInside: TouchDown: TouchDownRepeat: TouchUpInside: TouchDown: TouchDownRepeat: TouchUpInside:
116 - 长按会依次触发: TouchDown: buttonLongPress: TouchCancel:
117
*/
118
说到UIControlEvent,再谈下UIControl, UIControl继承于UIView, 可以接收事件响应,看Demo:
01
- (void)viewDidLoad
02
{
03
[super viewDidLoad];
04
// UIControl : UIView
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];
11
[control release];
12
}
13
14
- (void)controlViewClick_1
15
{
16
NSLog(@"%@", NSStringFromSelector(_cmd));
17
}
18
19
- (void)controlViewClick_2
20
{
21
NSLog(@"%@", NSStringFromSelector(_cmd));
22
}
23
24
- (void)controlViewClick_3
25
{
26
NSLog(@"%@", NSStringFromSelector(_cmd));
27
}
程序打印:
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