iOS UIButton之UIControlEvents介绍

级别:★★☆☆☆
标签:「UIButton」「UIControlEvents」
作者: WYW
审校: QiShare团队

大家好,今天小编带大家研究一下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截图

相关代码

#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的官方文档