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)
上一篇 2022年7月25日 上午10:46
下一篇 2022年7月25日 上午11:00


相关推荐

  • 【技术分享】Memcached介绍及php-memcache扩展安装

    【技术分享】Memcached介绍及php-memcache扩展安装目录 Memcached 和 php memcache 的区别 windows 环境下安装 MemcachedMem 相关命令 windows 环境下安装 php memcache 扩展 Memcached 和 php memcache 的区别 Memcached 技术 是内存缓存 PHP memcache 或者称为 memcache 是 PHP 的一个扩展 用于 php 管理 Memcached 理解 Memcached 就像是一个数据库一样 PHP 可以跟它连接交互 JAVA 可以 其他语言也可以 但是 PHP 有 PHP 的语法规则 Me

    2026年3月20日
    2
  • pycharm添加解释器失败_pycharm更新python解释器

    pycharm添加解释器失败_pycharm更新python解释器求大神告知:Pycharm添加Python解释器提示:CannotsetupapythonSDKatPython3.8(untitled5)(H:\Python\python-3.8.2-amd64.exe).TheSDKseemsinvalid.并且Python一直出现ModifySetup怎么解决?…

    2022年8月26日
    9
  • DMA控制器8237A「建议收藏」

    DMA控制器8237A「建议收藏」1DMA系统简介DMA(directmemoryaccess)是一种外设与存储器或者存储器与存储器之间直接传输数据的方式,在进行DMA存取时,CPU让出总线控制权,不在采用输入输出指令的方法进行数据存取,而采用一个专门的硬件DMAC(DirectMemoryAccessControl)控制电路,减少了中间环节,从而提高了传输速率。1.1DMA基本原理DMA直接实现I/O与存储器之间的数据传送。①当I/O接口准备好,希望进行DMA操作时,就像DMAC发出DMA请求信号DRQ(DMARe

    2022年5月18日
    58
  • 广州有哪些好点的软件外包公司或者软件开发公司呀?听说广州碧软还不错,还有其他靠谱的软件外包公司?

    广州有哪些好点的软件外包公司或者软件开发公司呀?听说广州碧软还不错,还有其他靠谱的软件外包公司?广州有哪些好点的软件外包公司或者软件开发公司呀?听说广州碧软还不错,还有其他靠谱的软件外包公司?广州碧软,做软件开发与外包还不错,我们和他们一直合作了好几年,不比那些超大型软件外包公司差,因为超大的软件外包公司公司他们也仅仅是一个分公司小部门来给你做项目,店大欺客啊,碧软他们规模中等,但用心,把该交付的东西做好很重要。…

    2022年5月8日
    130
  • DatabaseMetaData元数据

    DatabaseMetaData元数据通过java.sql.DatabaseMetaData接口,您可以获得有关您已连接到的数据库的元数据。例如,你可以看到哪些表的数据库,和什么中定义的列的每个表的数量,是否是给定功能支持等。DatabaseMetaData接口包含很多的方法,和并不是所有将在本教程中覆盖。你应该看看的JavaDoc。此文本将只是覆盖面不够,给你一种感觉,你可以用它。获得一个DatabaseMetaData实例

    2022年6月19日
    26
  • python数据分析入门笔记[1]

    python数据分析入门笔记[1](五)读写SQL数据库ps.数据库的代码是我直接从网络上粘贴过来的,没有测试过是不是可行,先贴上来。数据库我还在摸索中,学习心得学习笔记之类的大家可以一起分享23333~(二)数据排序(用的是t

    2022年7月5日
    20

发表回复

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

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