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()来识别。

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

  1. ios事件-触摸事件2(手势 和 pointInSide()、hitTest()、touchesBegan()、touchesMoved()、touchesEnded()的关系)

    ios事件-触摸事件2(手势 和 pointInSide().hitTest().touchesBegan().touchesMoved().touchesEnded().touchesCancell ...

  2. IOS中触摸事件学习

    IOS中触摸事件学习 1. 事件的声明周期 2. 系统相应阶段 3. APP响应阶段 4. 触摸.事件.响应者 4.1 UITouch(触摸) 4.2 UIEvent(事件真身) 4.3 UIResp ...

  3. iOS中触摸事件传递和响应原理

    系统响应阶段 1.手指触碰屏幕,屏幕感受到触摸后,将事件交由IOKit来处理. 2.IOKIT将触摸事件封装成IOHIDEvent对象,并通过mach port传递给SpringBoard进程. ma ...

  4. iOS开发触摸事件的传递

    1. iOS中的三种事件类型 触摸事件.加速计事件.远程事件. 触摸事件:通过触摸.手势进行触发(例如手指点击.缩放) 加速计事件:通过加速器进行触发(例如手机晃动,典型应用是微信摇一摇) 远程事件: ...

  5. iOS 一一 触摸事件和手势

    iOS触摸事件和手势 文章出处:http://www.jianshu.com/p/cb0314b72883 在iOS中,触摸表示用户手指触击屏幕及在屏幕上移动时,系统不断发送给应用程序对象,一个UIT ...

  6. iOS:触摸事件、手势识别、摇晃事件、耳机线控

    概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事件(手势操作).运动事件. ...

  7. ScrollView的上下联动事件 触摸事件传递机制

    注意事项:1.当返回值为false的时候,触摸事件向下或者向子view传递: 2.当返回时间为true时:触摸事件不向下执行 常用事件的方法: dispatchTouchEvent(Acitivity ...

  8. IOS UITouch 触摸事件,UIResponder 的作用

    IOS 中事件响应皆是通过UIResponder.我们可以执行UIResponder协议响应事件.这里我们用UIViewController做示例,UIViewController默认实现UIResp ...

  9. Event Handling Guide for iOS——由触摸事件传递想到的

    先帖官方文档地址 https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlin ...

最新文章

  1. Windows Phone 开发工具包7.1公测版新特性
  2. 老板用人5大原则,能力绝不是首选!大多数人的想法是错误的
  3. 华为服务器怎么查看系统日志,服务器运行日志查看
  4. jQuery对象的序列化详解
  5. 被美国主流投资平台看好,虎牙缘何能在上市大军中脱颖而出?
  6. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
  7. 从容器到微服务,技术架构、网络和生态详解
  8. 为什么美元持续贬值,国际市场上价格与美元挂钩的大宗商品,包括石油、钢铁、铜、矿石、粮食等,价格上涨
  9. Java中的queue和deque对比详解
  10. java AST 表达式_Atitti.java exp ast java表达式语法ast构造器
  11. 2.3 Hadoop安装与体验
  12. centos7查看当前系统时间、_CentOS 7修改系统时间及硬件时间
  13. NVIDIA NSight System工具安装和使用介绍(MacOS)
  14. TexWorks 拼写检查
  15. Zabbix监控系统系列之九:监控网络设备指定接口流量
  16. AutoCAD插件实现帐号密码登录
  17. k8s滚动更新(六)--技术流ken
  18. 【mysql innodb索引结构B+树】
  19. 芯片破壁者(十六):德州仪器的“罗生门”
  20. 苹果G5机箱改造 (螺帽法)

热门文章

  1. java获取当前年份、月份和日期字符串等
  2. phpstud使用教程_phpstudy使用说明
  3. android 评测软件,安卓评测app下载 安卓评测软件 for Android v8.4.3 安卓版 下载-脚本之家...
  4. get tweets by twitter api
  5. Vue 获取元素高度总是不准确的问题
  6. 【codeforces round#800 D题 Fake Plastic Trees】树上贪心
  7. 【数据结构与算法】之深入解析“有效的正方形”的求解思路与算法示例
  8. linux查看内存条ddr3和ddr4,DDR3和DDR4有哪些区别,该如何选择呢?
  9. 邯郸市计算机中专学校地址,邯郸市中专学校一览表
  10. C#.net winform skin 皮肤大全