ios事件-触摸事件3(UIButton 和 pointInSide()、hitTest()、touchesBegan()、touchesMoved()、touchesEnded()的关系)

ios事件-触摸事件3(UIButton 和 pointInSide()、hitTest()、touchesBegan()、touchesMoved()、touchesEnded()的关系)ios事件-触摸事件3(UIButton和pointInSide()、hitTest()、touchesBegan()、touchesMoved()、touchesEnded()、touchesCancelled()的关系)先看效果图本文中,凡是看到xxx(),即表示xxx是一个函数或者方法!!!事件分为事件传递和事件响应,其中,事件响应又称事件处理。具体代码ButtonVC的代码…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

ios事件-触摸事件3(UIButton 和 pointInSide()、hitTest()、touchesBegan()、touchesMoved()、touchesEnded()、touchesCancelled()的关系)

先看效果图

在这里插入图片描述
本文中,凡是看到xxx(),即表示xxx是一个函数或者方法!!!事件分为事件传递和事件响应,其中,事件响应又称事件处理。

具体代码

ButtonVC的代码如下:

@interface ButtonVC : UIViewController
@end
//--------分隔符,分隔.h文件和.m文件-------------
#import "ButtonVC.h"
#import "MyButton.h"

@interface ButtonVC ()

@end
//--------分隔符,分隔.h文件和.m文件-------------
@implementation ButtonVC
/**
 1、button的点击和pointInSide()、hitTest()的关系:在button的hitTest()中返回button实例,这个button才能响应事件
 2、button不同事件的识别,也是通过touchesBegan()、touchesMoved()、touchesEnd()和touchesCanceled()来识别,所以如果你在你自定义的button里面的重写的touchesBegan: withEvent:方法中不调用[super touchesBegan:touches withEvent:event];时,button的监听方法(在本例中为clicka:withEvent:方法)就不会被调用!
 3、Button的addTarget:action:forControlEvents方法的forControlEvents的参数有UIControlEventTouchUpInside、UIControlEventTouchDown。
    其中UIControlEventTouchDown表示@selector(clicka:withEvent:)方法在Button的touchesBegan:withEvent:方法之后以及touchesMoved:withEvent:方法之前调用。
    UIControlEventTouchUpInside表示@selector(clicka:withEvent:)方法在Button的touchesEnded:withEvent:方法之后调用。
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColor.whiteColor;
    MyButton *button = [MyButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100.f, 100.f, 100.f, 100.f);
    button.backgroundColor = UIColor.redColor;
    [button setTitle:@"一个button" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clicka:withEvent:) forControlEvents:UIControlEventTouchDown]; //添加监听,监听对象是self,监听策略是UIControlEventTouchDown,监听方法是clicka:withEvent:
    
    [button sendActionsForControlEvents:UIControlEventTouchDown];//即使没有[self.view addSubview:button], 下面的clicka()也会被调用
    
    [self.view addSubview:button];
    
}

- (void)clicka:(MyButton *)button withEvent:(UIEvent *)event {
    NSLog(@"你点击了button");
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"vc, %s",  __func__);
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"vc, %s",  __func__);
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"vc, %s", __func__);
    [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"vc, %s", __func__);
    [super touchesCancelled:touches withEvent:event];
}

@end

MyButton的代码如下:

#import <UIKit/UIKit.h>
@interface MyButton : UIButton
@end
//--------分隔符,分隔.h文件和.m文件-------------
@implementation MyButton
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"%s", __func__);
    return [super pointInside:point withEvent:event];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"%s", __func__);
    
    return [super hitTest:point withEvent:event];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s", __func__);
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s", __func__);
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s", __func__);
    [super touchesEnded:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s", __func__);
    [super touchesCancelled:touches withEvent:event];
}
@end

操作场景

  1. 进入ButtonVC的界面时,还没有做任何操作,输出结果如下:
2019-08-31 14:58:24.239067+0800 E03事件层次分析[29333:9860811] 你点击了button

分析:在ButtonVC的viewDidLoad()中调用了[button sendActionsForControlEvents:UIControlEventTouchDown]; ,该方法会调用监听策略为UIControlEventTouchDown的监听对象的监听方法,在本例中调用的是ButtonVC(监听对象)的clicka: withEvent:方法(监听方法)。

  1. 在红色按钮的区域内点击一下,输出结果如下:
2019-08-31 15:04:47.589038+0800 E03事件层次分析[29333:9860811] -[MyButton hitTest:withEvent:]
2019-08-31 15:04:47.589239+0800 E03事件层次分析[29333:9860811] -[MyButton pointInside:withEvent:]
2019-08-31 15:04:47.590528+0800 E03事件层次分析[29333:9860811] -[MyButton touchesBegan:withEvent:]
2019-08-31 15:04:47.590724+0800 E03事件层次分析[29333:9860811] 你点击了button
2019-08-31 15:04:47.723649+0800 E03事件层次分析[29333:9860811] -[MyButton touchesEnded:withEvent:]

分析:button的UIControlEventTouchDown事件的识别,是通过touchesBegan()来识别。

如果把ButtonVC的viewDidLoad()里面的[button addTarget:self action:@selector(clicka:withEvent:) forControlEvents:UIControlEventTouchDown];改为[button addTarget:self action:@selector(clicka:withEvent:) forControlEvents:UIControlEventTouchUpInside];,然后把MyButton中的[super touchesEnded:touches withEvent:event];删掉,那么 在红色按钮的区域内点击一下,输出结果如下:

2019-08-31 15:08:21.163142+0800 E03事件层次分析[29630:9884894] -[MyButton hitTest:withEvent:]
2019-08-31 15:08:21.163395+0800 E03事件层次分析[29630:9884894] -[MyButton pointInside:withEvent:]
2019-08-31 15:08:21.164825+0800 E03事件层次分析[29630:9884894] -[MyButton touchesBegan:withEvent:]
2019-08-31 15:08:21.241352+0800 E03事件层次分析[29630:9884894] -[MyButton touchesEnded:withEvent:]

说明:button的UIControlEventTouchUpInside事件的识别,是通过touchesBegan和touchesEnded()来识别。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/164188.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • NAT模式实现局域网物理机与虚拟机的互通访问「建议收藏」

    NAT模式实现局域网物理机与虚拟机的互通访问「建议收藏」玩过虚拟机的朋友都知道,不管是vbox还是vm,最常用的网络设置也不外乎3种:1、桥接模式:此模式下,虚拟机的操作系统就像和物理机同一段网络中的物理机一样,它可以访问网络中的任何机器,同时只要物理机可以访问网络,虚拟机也可以实现上网。此模式是懒人模式首选!但换来一个问题就是,如果你的物理机网络IP发生变化,虚拟机的IP也会相应的改变。如果IP变化对虚拟机有影响的环境,此模式慎用!

    2022年6月23日
  • gbdt算法_双色球最简单的算法

    gbdt算法_双色球最简单的算法https://github.com/NLP-LOVE/ML-NLP/blob/master/Machine%20Learning/3.2%20GBDT/3.2%20GBDT.md1.解释一下GBDT算法的过程GBDT(GradientBoostingDecisionTree),全名叫梯度提升决策树,使用的是Boosting的思想。1.1Boosting思想Boosting方法训练基分类器时采用串行的方式,各个基分类器之间有依赖。它的基本思路是将基分类器层层叠加,每一层在训练的时候,对前一

    2022年10月12日
  • 新经济政策商品经济_2021年国际商务热点

    新经济政策商品经济_2021年国际商务热点新经济的50个新热点2005-06-2114:09  中国机电企业网10多年来,新经济一直沿着“技术发展”和“应用创新”这两个方向奔跑。  这两个方向可以被看作既分立又统一的一对“双螺旋”——技术发展创造了应用的创新环境,而应用的创新往往很快就会触到技术的极限,进而鞭策技术的进一步演进。  在技术和应用这两条线上都可以诞生出引人入胜的商业创意,但是只有当技术和应用的激烈碰撞达到一定的融合程

    2022年10月5日
  • ZigBee协议栈简介和流程「建议收藏」

    ZigBee协议栈简介和流程「建议收藏」ZigBee协议栈实际上就是ZigBee协议的API接口一般步骤为:1.组网:调用协议栈的组网函数、加入网络函数,实现网络的建立与节点的加入2.发送:发送节点调用协议栈的无线数据发送函数,实现无线数据发送3.接收:接收节点调用协议栈的无线数据接收函数,实现无线数据接收大致流程:main()→osal_init_system()→osalInitTasks()→SampleAp

    2022年5月8日
  • 苹果Mac允许安装未知来源的应用「建议收藏」

    苹果Mac允许安装未知来源的应用「建议收藏」打开终端,输入命令密码是你的开机密码允许安装未知来源的应用允许sudospctl–master-disable不允许sudospctl–master-enable

    2022年10月22日
  • PECP协议_rtcp协议

    PECP协议_rtcp协议  1.PathComputationElementProtocol(PCEP) asasouthboundplugininONOS.路径计算单元协议,ONOS的南向接口协议之一。(来源https://wiki.onosproject.org/display/ONOS/PCEP+Protocol) 2.ONOS是专门面向服务提供商和企业骨干网的开源SDN网…

    2022年9月7日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号