当前位置: 首页 > news >正文

触摸事件之响应者链验证

上一博文提到查找上一响应者的方法,现在就通过代码测试验证一下。

一、首先看一下测试代码结构

myViews基础myView,myView继承UIView。黄色是myviews的对象,蓝色是myView对象,红色是添加的View。

二、代码

1.在AppDelagate.m中添加

2.在ViewController中


#import "ViewController.h"
#import "myView.h"
#import "myViews.h"
 
@interface ViewController ()
@property(nonatomic,strong)UIView *view1;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.tag=10003;
    self.view1=[[UIView alloc]init];
    self.view1.frame=CGRectMake(50, 100, 40, 40);
    self.view1.backgroundColor=[UIColor redColor];
    self.view1.tag=10001;
    [self.view addSubview:self.view1];
     
    myView *myview1=[[myView alloc]init];
    myview1.frame=CGRectMake(50, 100, 30, 30);
    myview1.backgroundColor=[UIColor blueColor];
    myview1.tag=10002;
//    userInteractionEnabled是否可以用户交互 YES可以响应点击事件
    myview1.userInteractionEnabled=YES;
    [self.view addSubview:myview1];
     
    myViews *myviews1=[[myViews alloc]init];
    myviews1.frame=CGRectMake(50, 100, 25, 25);
    myviews1.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:myviews1];
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    //判断点击的是哪一个View 可以通过tag值 或通过==判断
   // NSLog(@"Yes or No:%d",self.view1==touch.view);
  //  NSLog(@"%ld",touch.view.tag);
     
    NSLog(@"控制器触屏开始滑动");
    //tapCount点击次数
    NSLog(@"%ld",touch.tapCount);
     
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    //locationInView 相对于参数View的当前位置
    CGPoint loactionPoint=[touch locationInView:self.view1];
    NSLog(@"%@",NSStringFromCGPoint(loactionPoint));
    //locationInView 相对于参数View的前一位置
    CGPoint prePoint=[touch previousLocationInView:self.view1];
    NSLog(@"%@",NSStringFromCGPoint(prePoint));
    CGFloat moveX=loactionPoint.x-prePoint.x;
    CGFloat moveY=loactionPoint.y-prePoint.y;
    NSLog(@"%f %f",moveX,moveY);
    CGPoint temp=self.view1.center;
    temp.x+=moveX;
    temp.y+=moveY;
    self.view1.center=temp;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"控制器触屏滑动结束");
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    //一般应用在来电话等
    NSLog(@"控制器触屏取消");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end

在myView.m中


#import "myView.h"
 
@implementation myView
 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"子View触屏开始");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"子view触屏中");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"子view触屏结束");
}
 
@end

在myViews.m中


#import "myViews.h"
 
@implementation myViews
 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"子子View触屏开始");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"子子view触屏中");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"子子view触屏结束");
}
 
 
@end

 三、运行效果图

四、验证

1.依次点击黄、蓝、红、白

由上面运行结果可以看出:黄色、蓝色的响应者都是本身,红色未实现Touch方法,它的响应者为ViewController中的View,由于该View也未实现,但它属于是ViewController的View,所以它的响应者是ViewController.

2.注释myViews、ViewController中的Touch实现,依次点击黄、蓝、红、白

由于myViews、ViewController类中的Touch实现注释,黄色点击时找其父视图,则找到myView中的,而将控制器的Touch实现注释则红色、白色的响应者变成了Application,所以调用了Appdalegate中的Touch实现,类似父节点的查找。


相关文章:

  • imx6 PCIE使能加载ath9k无线网卡
  • 深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接(转载)
  • 聚焦“裂变”,又拍云推出直播云等多场景解决方案
  • Nginx负载平衡 + Tomcat + 会话存储Redis配置要点
  • 基于矩阵分解的推荐算法
  • 虚拟机安装OSX10.11.4+编译libVLC和VLC源码
  • 容器类组件介绍
  • Wine里的中文程序出现方块字的解决方法以及Wine快捷方式的命令格式
  • 写数据处理程序的总结201604
  • 代码复审和两人合作
  • Android 二维码 生成和识别(附Demo源码)
  • Centos7安装rabbitMQ3.6.0
  • django中数据库的相关操作
  • ZABBIX 监控 LINUX Nginx
  • Font Awesome使用指南
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • Android框架之Volley
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • css系列之关于字体的事
  • SpriteKit 技巧之添加背景图片
  • Transformer-XL: Unleashing the Potential of Attention Models
  • 程序员最讨厌的9句话,你可有补充?
  • 从零开始学习部署
  • 飞驰在Mesos的涡轮引擎上
  • 分享一份非常强势的Android面试题
  • 给第三方使用接口的 URL 签名实现
  • 简析gRPC client 连接管理
  • 聊聊directory traversal attack
  • 使用权重正则化较少模型过拟合
  • 数据结构java版之冒泡排序及优化
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (done) 两个矩阵 “相似” 是什么意思?
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (十一)图像的罗伯特梯度锐化
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (一)u-boot-nand.bin的下载
  • (转)ORM
  • ***监测系统的构建(chkrootkit )
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution
  • .NET 材料检测系统崩溃分析
  • .NET 设计模式—简单工厂(Simple Factory Pattern)
  • .net 设置默认首页
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明
  • .net中应用SQL缓存(实例使用)
  • /dev下添加设备节点的方法步骤(通过device_create)
  • /使用匿名内部类来复写Handler当中的handlerMessage()方法
  • ::前边啥也没有
  • @RestControllerAdvice异常统一处理类失效原因
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现