当前位置:网站首页>事件的传递和响应以及使用实例
事件的传递和响应以及使用实例
2022-06-12 13:26:00 【Li.CQ】
在项目中使用响应链的地方
1. 在scrollView 中添加 slider
// - 方案一
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
if (CGRectContainsPoint(self.subView.frame, point)) {
self.scrollEnabled = NO;
return YES;
}
self.scrollEnabled = YES;
return YES;
}
// - 方案二
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if (CGRectContainsPoint(self.subView.frame, point)) {
self.scrollEnabled = NO;
return [super hitTest:point withEvent:event];
}
self.scrollEnabled = YES;
return [super hitTest:point withEvent:event];
}
// - 方案三
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if([view isKindOfClass:[UISlider class]]) {
self.scrollEnabled = NO;
} else {
self.scrollEnabled = YES;
}
return view;
}
2. 设置 scrollview 做的选项卡的 view 在第一个选项上是可以左滑返回
// - 设置 scrollview 做的选项卡的 view 在第一个选项上是可以左滑返回
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if ([[super hitTest:point withEvent:event] isKindOfClass:[UIControl class]]) {
return [super hitTest:point withEvent:event];
}else{
if ((self.contentOffset.x == 0) && (point.x < 30)) {
return nil;
}
return [super hitTest:point withEvent:event];
}
}
3. 让事件穿透view
// - 设置事件可以穿透 view
@implementation DYContainerBgView
/** 可以透过事件 */
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if ([super hitTest:point withEvent:event] == self) return nil;
return [super hitTest:point withEvent:event];
}
@end
4. 事件传递的解析
- 名词解释
- 事件传递 : 从上到下的顺序 (UIApplication–>UIWindow–>递归找到最合适处理的View),即从父控件到子控件的寻找过程 (事件传递最重要的就是找到合适的View来接收事件, 从父视图到子视图查找, 同一父视图, 从后加入到父视图 到 先加入到view的顺序遍历);
- 事件响应 : 从下到上的顺序 (实现View的touchesBegan方法,如果该View不能处理事件,则向上传递给它的父控件),从子控件传递给父控件(找到合适的View之后就会调用该View的touches方法进行响应处理具体的事件,如果当前的view 不响应touch 方法, 就查找上一响应者(superviwe, 或者view的控制器)来响应事件);
// - 示例代码 :
- (void)viewDidLoad {
[super viewDidLoad];
QGView *v1 = [[QGView alloc]initWithFrame:CGRectMake(90, 90, 200, 200)];
v1.backgroundColor = [UIColor redColor];
v1.testStr = @"v1";
[self.view addSubview:v1];
QGView *v2 = [[QGView alloc]initWithFrame:CGRectMake(90, 90, 180, 180)];
v2.backgroundColor = [UIColor greenColor];
v2.testStr = @"v2";
[self.view addSubview:v2];
QGView *v3 = [[QGView alloc]initWithFrame:CGRectMake(90, 90, 160, 160)];
v3.testStr = @"v3";
v3.backgroundColor = [UIColor redColor];
[self.view addSubview:v3];
QGView *v31 = [[QGView alloc]initWithFrame:CGRectMake(0, 0, 120, 120)];
v31.backgroundColor = [UIColor greenColor];
v31.testStr = @"v31";
[v3 addSubview:v31];
QGView *v32 = [[QGView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
v32.testStr = @"v32";
v32.backgroundColor = [UIColor redColor];
[v3 addSubview:v32];
QGView *v33 = [[QGView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
v33.backgroundColor = [UIColor greenColor];
v33.testStr = @"v33";
[v3 addSubview:v33];
}

在self.view.subView 数组中, 后添加的view, 越在数组靠后的元素, 在view中的层级越高, 越接近用户
// - 测试 :
1. 点击v3
2020-05-11 11:12:54.027766+0800 Demo_001[2008:56637] pointInside v3, ruturn : 1
2020-05-11 11:12:54.027995+0800 Demo_001[2008:56637] pointInside v33, ruturn : 0
2020-05-11 11:12:54.028168+0800 Demo_001[2008:56637] hitTest v33, ruturn : (null)
2020-05-11 11:12:54.028309+0800 Demo_001[2008:56637] pointInside v32, ruturn : 0
2020-05-11 11:12:54.028438+0800 Demo_001[2008:56637] hitTest v32, ruturn : (null)
2020-05-11 11:12:54.028569+0800 Demo_001[2008:56637] pointInside v31, ruturn : 0
2020-05-11 11:12:54.028674+0800 Demo_001[2008:56637] hitTest v31, ruturn : (null)
2020-05-11 11:12:54.029436+0800 Demo_001[2008:56637] hitTest v3, ruturn : <QGView: 0x7f9bede05ad0; frame = (90 90; 160 160); layer = <CALayer: 0x600003ec0780>>
2. 点击v33
2020-05-11 11:13:54.777979+0800 Demo_001[2008:56637] pointInside v3, ruturn : 1
2020-05-11 11:13:54.778181+0800 Demo_001[2008:56637] pointInside v33, ruturn : 1
2020-05-11 11:13:54.778399+0800 Demo_001[2008:56637] hitTest v33, ruturn : <QGView: 0x7f9bede0ca40; frame = (0 0; 80 80); layer = <CALayer: 0x600003ec0aa0>>
2020-05-11 11:13:54.778587+0800 Demo_001[2008:56637] hitTest v3, ruturn : <QGView: 0x7f9bede0ca40; frame = (0 0; 80 80); layer = <CALayer: 0x600003ec0aa0>>
3. 点击v31
2020-05-11 11:15:13.166848+0800 Demo_001[2008:56637] pointInside v3, ruturn : 1
2020-05-11 11:15:13.167039+0800 Demo_001[2008:56637] pointInside v33, ruturn : 0
2020-05-11 11:15:13.167145+0800 Demo_001[2008:56637] hitTest v33, ruturn : (null)
2020-05-11 11:15:13.167275+0800 Demo_001[2008:56637] pointInside v32, ruturn : 0
2020-05-11 11:15:13.167396+0800 Demo_001[2008:56637] hitTest v32, ruturn : (null)
2020-05-11 11:15:13.167503+0800 Demo_001[2008:56637] pointInside v31, ruturn : 1
2020-05-11 11:15:13.167669+0800 Demo_001[2008:56637] hitTest v31, ruturn : <QGView: 0x7f9bede08420; frame = (0 0; 120 120); layer = <CALayer: 0x600003ec0a40>>
2020-05-11 11:15:13.167813+0800 Demo_001[2008:56637] hitTest v3, ruturn : <QGView: 0x7f9bede08420; frame = (0 0; 120 120); layer = <CALayer: 0x600003ec0a40>>
4. 点击v1
2020-05-11 11:16:01.172527+0800 Demo_001[2008:56637] pointInside v3, ruturn : 0
2020-05-11 11:16:01.172711+0800 Demo_001[2008:56637] hitTest v3, ruturn : (null)
2020-05-11 11:16:01.172835+0800 Demo_001[2008:56637] pointInside v2, ruturn : 0
2020-05-11 11:16:01.172931+0800 Demo_001[2008:56637] hitTest v2, ruturn : (null)
2020-05-11 11:16:01.173068+0800 Demo_001[2008:56637] pointInside v1, ruturn : 1
2020-05-11 11:16:01.173249+0800 Demo_001[2008:56637] hitTest v1, ruturn : <QGView: 0x7f9bede06520; frame = (90 90; 200 200); layer = <CALayer: 0x600003ec01e0>>
结论 : 当前的vc的所有subView, 自外向内调用; 再从找到view的subView中, 自外向内调用
边栏推荐
- A brief introduction to Verilog mode
- STM32F1与STM32CubeIDE编程实例-设备驱动-DHT11温度温度传感器驱动
- 1001:Hello,World
- 2065: [example 2.2] sum of integers
- 达梦数据库DM8 windows环境安装
- C language [23] classic interview questions [2]
- 软件构造 03 正则表达式
- import torch_geometric 加载一些常见数据集
- 【刷题篇】超级洗衣机
- Stm32f1 and stm32subeide programming example - device driver -dht11 temperature sensor driver
猜你喜欢

Pre research of image scanning tool

Further understanding of the network

Embedded driver design
![2061: [example 1.2] trapezoidal area](/img/83/79b73ca10615c852768aba8d2a5049.jpg)
2061: [example 1.2] trapezoidal area

Hardware composition of embedded system - introduction of embedded development board based on ARM

Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题

442 authors, 100 pages! It took Google 2 years to release the new benchmark big bench | open source

关于#SQLite写注册功能时,数据表查询出错#的问题,如何解决?
![[embedded] serial communication and its case](/img/5c/2e691e5ef03c7d65fd514e8b940e7b.jpg)
[embedded] serial communication and its case

安装MySQL时出错,照着下面这个链接,做到cmd就不行了
随机推荐
基于华为云鲲鹏弹性云服务器ECS部署openGauss数据库【这次高斯不是数学家】
Dameng database DM8 Windows environment installation
torch_geometric message passing network
[wechat applet development] Part 1: development tool installation and program configuration
Implementing tensorflow deep learning framework similarflow with numpy
view的子视图的递归
The problem of Joseph in Informatics
[embedded] serial communication and its case
import torch_ Geometric loads some common datasets
单向环形链表实现约瑟夫环
Record some settings for visual studio 2019
【云原生 | Kubernetes篇】深入了解Ingress
Semantic segmentation with pytorch
Introduction to application design scheme of intelligent garbage can voice chip, wt588f02b-8s
Redis message queue repeated consumption
IC chip scheme fs4062b for lithium battery charging with 5V boost to 12.6V
Hardware composition of embedded system - introduction of embedded development board based on ARM
Script import CDN link prompt net:: err_ FILE_ NOT_ Found problem
Teach you how to create SSM project structure in idea
成功定级腾讯T3-2,万字解析