級別:★★☆☆☆
標籤:「UIButton」「UIControlEvents」
做者: WYWphp
你們好,今天小編帶你們研究一下UIButton
裏 各類UIControlEvents
的具體區別。
首先,咱們先看下蘋果官方對UIControlEvents
的定義。git
typedef NS_OPTIONS(NSUInteger, 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.
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 // 至關於上邊的全部值的 或
};
複製代碼
UIControlEventTouchDown
官方:A touch-down event in the control.
解釋:觸下control 中的事件(這個能夠用於監測 剛剛按下按鈕 或者是UISlider的時候的事件)
github
UIControlEventTouchDownRepeat
官方:A repeated touch-down event in the control; for this event the value of the UITouch tapCount method is greater than one.
解釋:在control上重複地按下的事件 這個事件的tap數量大於1
bash
UIControlEventTouchDragInside
官方: An event where a finger is dragged inside the bounds of the control.
解釋:手指在control的bounds範圍內拖動的的事件
微信
UIControlEventTouchDragOutside
官方: An event where a finger is dragged just outside the bounds of the control.
解釋:當手指拖動恰好在control的bounds 範圍外的事件
session
UIControlEventTouchDragEnter
官方:An event where a finger is dragged into the bounds of the control.
解釋:當手指拖動進入control範圍內的事件
app
UIControlEventTouchDragExit
官方: An event where a finger is dragged from within a control to outside its bounds.
解釋:當手指從control範圍內到它的bounds外的時候的事件
ide
UIControlEventTouchUpInside
官方:A touch-up event in the control where the finger is inside the bounds of the control.
解釋:手指在在control內部 觸發的touch-up事件(常常給按鈕添加這個事件)
測試
UIControlEventTouchUpOutside
官方:A touch-up event in the control where the finger is outside the bounds of the control.
解釋:手指在在control外部 觸發的touch-up事件
ui
UIControlEventTouchCancel
官方: A system event canceling the current touches for the control.
解釋:一種系統事件 取消control當前觸摸的事件
UIControlEventValueChanged
官方:A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.
解釋:拖動觸摸 或 其餘操做一個control引發這個control顯示一系列不一樣的值(像UISlider在拖動的時候值的變化能夠經過這個事件來監測)
UIControlEventPrimaryActionTriggered
官方: A semantic action triggered by buttons.
解釋:按鈕觸發的語義動做? 這個沒用過
UIControlEventEditingDidBegin
官方:A touch initiating an editing session in a UITextField object by entering its bounds.
解釋:當觸摸UITextField對象後 經過進入它的bounds 初始化一個編輯會話
UIControlEventEditingChanged
官方:A touch making an editing change in a UITextField object.
解釋:觸摸UITextField對象後一個編輯改變
UIControlEventEditingDidEnd
官方:A touch ending an editing session in a UITextField object by leaving its bounds.
解釋:在手指離開TextFiled對象的bounds的時候 觸摸結束的一個編輯會話
UIControlEventEditingDidEndOnExit
官方:A touch ending an editing session in a UITextField object.
解釋:在UITextField對象中 觸摸結束編輯會話
UIControlEventAllTouchEvents
官方:All touch events.
解釋:全部的觸摸事件
UIControlEventAllEditingEvents
官方:All editing touches for UITextField objects.
解釋: 對於UITextFiled對象的全部的的編輯觸摸
UIControlEventApplicationReserved
官方:A range of control-event values available for application use.
解釋: 爲應用使用的預留的 一系列可用的的control-event值
UIControlEventSystemReserved
官方:A range of control-event values reserved for internal framework use.
解釋: 爲內部framework預留的 一系列control-event values
UIControlEventAllEvents
官方:All events, including system events.
解釋:全部的事件 包括系統事件
target
爲nil
,點擊按鈕後方法正常執行,在響應鏈上找一個對象響應消息;action
爲null
或者是 方法寫錯名字 會崩潰 unrecognized selector sent to instance 0x7fef757063e0'
;UIControlEventAllEvents
;kDisplaceStep = 0
;Demo經過遍歷UIControlEvents 位移枚舉,找出按鈕添加了那些方法。
編譯器Demo截圖:
相關代碼
#import "QiButton_UIControlEventsViewController.h"
static NSUInteger kDisplaceStep = 0; //!< 偏移位數
static long long const kDisplacementBase = 0x01; //!< 偏移基數
@implementation QiButton_UIControlEventsViewController {
NSDictionary *_controlEventDictionary; //!< UIControlEvents 枚舉字典
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"UIControlEvents";
self.view.backgroundColor = [UIColor whiteColor];
[self prepareData];
[self controlEventsDemo];
}
#pragma mark - private functions
- (void)prepareData {
_controlEventDictionary = @{
@(UIControlEventTouchDown) : @"UIControlEventTouchDown",
@(UIControlEventTouchDownRepeat) : @"UIControlEventTouchDownRepeat",
@(UIControlEventTouchDragInside) : @"UIControlEventTouchDragInside",
@(UIControlEventTouchDragOutside) : @"UIControlEventTouchDragOutside",
@(UIControlEventTouchDragEnter) : @"UIControlEventTouchDragEnter",
@(UIControlEventTouchDragExit) : @"UIControlEventTouchDragExit",
@(UIControlEventTouchUpInside) : @"UIControlEventTouchUpInside",
@(UIControlEventTouchUpOutside) : @"UIControlEventTouchUpOutside",
@(UIControlEventTouchCancel) : @"UIControlEventTouchCancel",
@(UIControlEventValueChanged) : @"UIControlEventValueChanged",
@(UIControlEventPrimaryActionTriggered) : @"UIControlEventPrimaryActionTriggered",
@(UIControlEventEditingDidBegin):@"UIControlEventEditingDidBegin",
@(UIControlEventEditingChanged) : @"UIControlEventEditingChanged",
@(UIControlEventEditingDidEnd):@"UIControlEventEditingDidEnd",
@(UIControlEventEditingDidEndOnExit):@"UIControlEventEditingDidEndOnExit",
@(UIControlEventAllTouchEvents):@"UIControlEventAllTouchEvents",
@(UIControlEventAllEditingEvents):@"UIControlEventAllEditingEvents",
@(UIControlEventApplicationReserved):@"UIControlEventApplicationReserved",
@(UIControlEventSystemReserved):@"UIControlEventSystemReserved",
@(UIControlEventAllEvents):@"UIControlEventAllEvents",
};
}
- (void)controlEventsDemo {
// - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
/**
* Demo思路 建立5個按鈕 而且添加事件處理
* 1. 中規中矩建立按鈕添加事件 而且添加事件處理 點擊按鈕後 方法正常執行
* 2. target 設置爲nil 點擊按鈕後方法正常執行 在響應鏈上找一個對象響應消息
* 3. action 設置爲null 或者是 方法寫錯名字 會崩潰 unrecognized selector sent to instance 0x7fef757063e0'
* 4. 給按鈕添加 事件 UIControlEventAllEvents
* 5. 復位按鈕 當前功能是設置 kDisplaceStep = 0;
查看效果:
* 6. 讀者能夠試着改變UIControlEvents 位移枚舉 輸出爲按鈕添加的UIControlEvents的內容
* 其中還有別的內容像 UITextField 像 UISlider 中的一些事件處理
*/
self.edgesForExtendedLayout = UIRectEdgeNone;
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
if (screenH == 812.0 && screenW == 375.0) {
screenH -= 122.0;
}else {
screenH -= 64.0;
}
CGFloat btnTopMargin = 20.0;
CGFloat btnW = [UIScreen mainScreen].bounds.size.width;
CGFloat btnH = (screenH - (btnTopMargin * 5)) / 5;
UIButton *normalBtn = [[UIButton alloc] initWithFrame:CGRectMake(.0, btnTopMargin, btnW, btnH)];
[self.view addSubview:normalBtn];
[normalBtn setTitle:@"normalButton" forState:UIControlStateNormal];
normalBtn.backgroundColor = [UIColor lightGrayColor];
[normalBtn addTarget:self action:@selector(normalButtonClicked:) forControlEvents:UIControlEventTouchDown];
[normalBtn addTarget:self action:@selector(normalButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIButton *targetNilBtn = [[UIButton alloc] initWithFrame:CGRectMake(.0, (btnTopMargin * 2 + btnH), btnW, btnH)];
[self.view addSubview:targetNilBtn];
[targetNilBtn setTitle:@"targetNilButton" forState:UIControlStateNormal];
targetNilBtn.backgroundColor = [UIColor grayColor];
[targetNilBtn addTarget:nil action:@selector(targetNilButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[targetNilBtn addTarget:nil action:@selector(targetNilButtonClicked:) forControlEvents:UIControlEventTouchDown];
UIButton *selectorNullBtn = [[UIButton alloc] initWithFrame:CGRectMake(.0, (btnTopMargin * 3 + btnH * 2), btnW, btnH)];
[self.view addSubview:selectorNullBtn];
[selectorNullBtn setTitle:@"null Selector Button" forState:UIControlStateNormal];
selectorNullBtn.backgroundColor = [UIColor darkGrayColor];
[selectorNullBtn addTarget:self action:@selector(null) forControlEvents:UIControlEventTouchUpInside];
UIButton *allEventsBtn = [[UIButton alloc] initWithFrame:CGRectMake(.0, (btnTopMargin * 4 + btnH * 3), btnW, btnH)];
[self.view addSubview:allEventsBtn];
[allEventsBtn setTitle:@"allEventsButton" forState:UIControlStateNormal];
allEventsBtn.backgroundColor = [[UIColor darkTextColor] colorWithAlphaComponent:0.6];
[allEventsBtn addTarget:self action:@selector(allEventButtonClicked:) forControlEvents:UIControlEventAllEvents];
UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(.0, (btnTopMargin * 5 + btnH * 4), btnW, btnH)];
[self.view addSubview:resetBtn];
[resetBtn setTitle:@"復位" forState:UIControlStateNormal];
resetBtn.backgroundColor = [[UIColor darkTextColor] colorWithAlphaComponent:0.8];
[resetBtn addTarget:self action:@selector(resetButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - action functions
- (void)normalButtonClicked:(UIButton *)sender {
UIControlEvents tempEvents = sender.allControlEvents;
for (; kDisplaceStep < 32; ++ kDisplaceStep) {
if (tempEvents & (kDisplacementBase << kDisplaceStep)) {
NSLog(@"添加的allControlEvents:%lu",(unsigned long)sender.allControlEvents);
NSLog(@"分別爲: %llu--%@", (kDisplacementBase << kDisplaceStep),_controlEventDictionary[@(kDisplacementBase << kDisplaceStep)]); // %o %x
// 65 至關於 UIControlEventTouchDown | UIControlEventTouchUpInside
}
}
NSLog(@"%s",__FUNCTION__);
}
- (void)targetNilButtonClicked:(UIButton *)sender {
NSLog(@"添加的allControlEvents:%lu",(unsigned long)sender.allControlEvents);
NSLog(@"%s",__FUNCTION__);
// 調用次數爲2 是由於測試的UIControlEventTouchDown 和 UIControlEventTouchUpInside 各調用了一次
}
- (void)allEventButtonClicked:(UIButton *)sender {
UIControlEvents tempEvents = sender.allControlEvents;
for (; kDisplaceStep < 32; ++ kDisplaceStep) {
if (tempEvents & (kDisplacementBase << kDisplaceStep)) {
NSLog(@"添加的allControlEvents:%lu",(unsigned long)sender.allControlEvents);
NSLog(@"可能有: %llu--%@", (kDisplacementBase << kDisplaceStep),_controlEventDictionary[@(kDisplacementBase << kDisplaceStep)]); // %o %x
}
}
// 其輸出結果表明其能夠響應不少事件,這種狀況下就不可以準確的肯定是那個事件了, 由於多是彼此之間二進制位重複的值 作的 或 操做後的結果。
NSLog(@"%s",__FUNCTION__);
}
- (void)resetButtonClicked:(UIButton *)sender {
NSLog(@"%s",__FUNCTION__);
kDisplaceStep = 0;
}
@end
複製代碼
關注咱們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow) QiShare(微信公衆號)