iOS UIButton之UIControlEvents介紹

QiShare發表於2018-08-13

級別:★★☆☆☆
標籤:「UIButton」「UIControlEvents」
作者: WYW

大家好,今天小編帶大家研究一下UIButton裡 各種UIControlEvents的具體區別。
首先,我們先看下蘋果官方對UIControlEvents的定義。

UIControlEvents 相關位移列舉

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   // 相當於上邊的所有值的 或
    };
複製程式碼

UIControlEvents的具體解釋

  • UIControlEventTouchDown
    官方:A touch-down event in the control.
    解釋:觸下control 中的事件(這個可以用於監測 剛剛按下按鈕 或者是UISlider的時候的事件)

  • 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

  • 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 範圍外的事件

  • UIControlEventTouchDragEnter
    官方:An event where a finger is dragged into the bounds of the control.
    解釋:當手指拖動進入control範圍內的事件

  • UIControlEventTouchDragExit
    官方: An event where a finger is dragged from within a control to outside its bounds.
    解釋:當手指從control範圍內到它的bounds外的時候的事件

  • 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事件

  • 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.
    解釋:所有的事件 包括系統事件

Demo研究:

思路: 設計了5個按鈕 用不同方式新增事件處理(控制變數法)
  • 第一個Button:建立按鈕新增事件,並且新增事件處理,點選按鈕後方法正常執行;
  • 第二個Button:控制 targetnil,點選按鈕後方法正常執行,在響應鏈上找一個物件響應訊息;
  • 第三個Button:控制 actionnull 或者是 方法寫錯名字 會崩潰 unrecognized selector sent to instance 0x7fef757063e0';
  • 第四個Button:給按鈕新增 事件 UIControlEventAllEvents
  • 第五個Button: 復位按鈕 當前功能是設定 kDisplaceStep = 0;

Demo通過遍歷UIControlEvents 位移列舉,找出按鈕新增了那些方法。

編譯器Demo截圖

iOS UIButton之UIControlEvents介紹

相關程式碼


#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

複製程式碼

程式碼GitHub地址

特別緻謝:

蘋果關於UIControlEvents的官方文件

關注我們的途徑有:
QiShare(簡書)
QiShare(掘金)
QiShare(知乎)
QiShare(GitHub)
QiShare(CocoaChina)
QiShare(StackOverflow)
QiShare(微信公眾號)

相關文章