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


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,511评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,495评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,595评论 0 225
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,558评论 0 190
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,715评论 3 270
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,672评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,112评论 2 291
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,837评论 0 181
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,417评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,928评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,316评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,773评论 2 234
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,253评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,827评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,440评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,523评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,583评论 2 249