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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java异常处理之throw, throws,try和catch[通俗易懂]

    java异常处理之throw, throws,try和catch[通俗易懂]   程序运行过程中可能会出现异常情况,比如被0除、对负数计算平方根等,还有可能会出现致命的错误,比如内存不足,磁盘损坏无法读取文件等,对于异常和错误情况的处理,统称为异常处理。   Java异常处理主要通过5个关键字控制:try、catch、throw、throws和finally。try的意思是试试它所包含的代码段中是否会发生异常;而catch当有异常时抓住它,并进行相应的处理,使程序不受

    2022年5月12日
    33
  • 激光SLAM算法学习(三)——3D激光SLAM

    激光SLAM算法学习(三)——3D激光SLAM3D激光SLAM1、3D激光SLAM的介绍3D激光SLAM的输入:IMU数据3D激光雷达数据里程计数据3D激光SLAM的输出:3D点云地图机器人的轨迹orPoseGraph2、3D激光SLAM的发展3D激光SLAM的帧间匹配方法——点云配准算法Point-to-PlaneICPFeature-basedMethod3D激光SLAM的回环检测方法Scan-to…

    2022年8月23日
    2
  • Numpy中Meshgrid函数介绍及2种应用场景

    Numpy中Meshgrid函数介绍及2种应用场景近期在好几个地方都看到meshgrid的使用,虽然之前也注意到meshgrid的用法。但总觉得印象不深刻,不是太了解meshgrid的应用场景。所以,本文将进一步介绍Numpy中meshgrid的用法

    2022年7月1日
    17
  • Java基础知识总结(超详细整理),java从入门到精通pdf「建议收藏」

    Java基础知识总结(超详细整理),java从入门到精通pdf「建议收藏」数组数组是数据的集合,一个容器,用来存储任何类型的数据,包括原始数据类型和引用数据类型,但是一旦指定了数组的类型之后,就只能用来存储指定类型的数据。数组声明的三种方式:数据类型[]数组名=new数据类型[长度];数据类型[]数组名={数据,数据,…,数据};数据类型[]数组名=new数据类型长度[]{数据,数据,…,数据};一维数组:数组变量的声明:语法:数据类型[]数组名;,如:int[]num;、double[]d;、String[].

    2022年7月7日
    17
  • Java安全之RMI协议分析

    Java安全之RMI协议分析0x00前言在前面其实有讲到过RMI,但是只是简单描述了一下RMI反序列化漏洞的利用。但是RMI底层的实现以及原理等方面并没有去涉及到,以及RMI的各种攻击方式。在其

    2021年12月12日
    49
  • 魔方第五步式视频教程_fpga滤波算法

    魔方第五步式视频教程_fpga滤波算法完整版教程下载地址:事隔五年之后,开启第2版DSP数字信号处理和CMSIS-NN神经网络教程,同步开启三代示波器,更至49章(2021-09-20)-STM32H7-硬汉嵌入式论坛-PoweredbyDiscuz!http://www.armbbs.cn/forum.php?mod=viewthread&tid=94547第49章STM32F429的自适应滤波器实现,无需Matlab生成系数(支持实时滤波)本章节讲解LMS最小均方自适应滤波器实现,无需Matla…

    2022年9月4日
    2

发表回复

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

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