import UIKit

class FatherView: UIView {

override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {

print("Detect Touch Event")

if (self.hidden == false) && (alpha > 0) {

for subview in subviews {

if CGRectContainsPoint(subview.frame, point) {

if subview.isKindOfClass(ChildView1) {

print("view 1")

return subview

}

if subview.isKindOfClass(ChildView2) {

print("view 2")

return subview

}

}

}

}

return nil

}

}

import UIKit

class ChildView1: UIView {}

import UIKit

class ChildView2: UIView {}

import UIKit

class ViewController: UIViewController {

let father = FatherView()

let child1 = ChildView1()

let child2 = ChildView2()

let child3 = ChildView2()

override func viewDidLoad() {

super.viewDidLoad()

father.frame = CGRectMake(0, 0, 100, 100)

child1.frame = CGRectMake(20, 20, 20, 20)

child2.frame = CGRectMake(120, 120, 120, 120)

child3.frame = CGRectMake(200, 200, 120, 120)

//        father.clipsToBounds = true

father.backgroundColor = UIColor.grayColor()

child1.backgroundColor = UIColor.redColor()

child2.backgroundColor = UIColor.blueColor()

child3.backgroundColor = UIColor.greenColor()

view.addSubview(father)

father.addSubview(child1)

father.addSubview(child2)

father.addSubview(child3)

child1.addGestureRecognizer(

UITapGestureRecognizer(target: self, action: #selector(touchTest1)))

child2.addGestureRecognizer(

UITapGestureRecognizer(target: self, action: #selector(touchTest2)))

child3.addGestureRecognizer(

UITapGestureRecognizer(target: self, action: #selector(touchTest3)))

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

func touchTest1() {

print("test 1\n")

}

func touchTest2() {

print("test 2\n")

}

func touchTest3() {

print("test 3\n")

}

}

Swift - 触摸事件响应机制(UiView事件传递)的更多相关文章

  1. iOS-UIResponse之事件响应链及其事件传递

    UIResponse之事件响应链及其事件传递 我们的App与用户进行交互,基本上是依赖于各种各样的事件.一个视图是一个事件响应者,可以处理点击等事件,而这些事件就是在UIResponder类中定义的. ...

  2. 【iOS 开发】iOS 开发 简介 (IOS项目文件 | MVC 模式 | 事件响应机制 | Storyboard 控制界面 | 代码控制界面 | Retina 屏幕图片适配)

    一. iOS 项目简介 1. iOS 文件简介 创建一个 HelloWorld 项目, 在这个 IOS 项目中有四个目录 : 如下图; -- HelloWorldTests 目录 : 单元测试相关的类 ...

  3. Android MotionEvent事件响应机制

    在android中,事件主要包括点击.长按.拖曳.滑动等操作,这些构成了Android的事件响应,总体来说,所有的事件都由如下三个部分作为基础构成: 按下(action_down),移动(action ...

  4. cocos2d-x 事件分发机制 ——加速计事件监听

    加速计事件监听机制 在上一篇中介绍了cocos2d-x中的触摸事件机制,这篇来介绍下游戏中也常常常使用到的加速计事件,这些都是游戏中的常常要用到的. 移动设备上一个非常重要的输入源是设备的方向.大多数 ...

  5. cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析

    (一) Android平台下: cocos2dx 版本3.2,先导入一个android工程,然后看下AndroidManifest.xml <application android:label= ...

  6. 理解点击屏幕的事件响应--->对- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event方法的理解

    要理解这两个方法.先了解一下用户触摸屏幕后的事件传递过程. 当用户点击屏幕后,UIApplication 先响应事件,然后传递给UIWindow.如果window可以响应.就开始遍历window的su ...

  7. 理解点击屏幕的事件响应--->对UIView的hitTest: withEvent: 方法的理解

    要理解这两个方法.先了解一下用户触摸屏幕后的事件传递过程. 当用户点击屏幕后,UIApplication 先响应事件,然后传递给UIWindow.如果window可以响应.就开始遍历window的su ...

  8. 浅谈Android中的事件分发机制

    View事件分发机制的本质就是就是MotionEvent事件的分发过程,即MotionEvent产生后是怎样在View之间传递及处理的. 首先介绍一下什么是MotionEvent.所谓MotionEv ...

  9. 本以为精通Android事件分发机制,没想到被面试官问懵了

    文章中出现的源码均基于8.0 前言 事件分发机制不仅仅是核心知识点更是难点,并且还是View的一大难题滑动冲突解决方法的理论基础,因此掌握好View的事件分发机制是十分重要的. 一.基本认识 1. 事 ...

随机推荐

  1. each,collect map collect! map!

    arr = [1,2,3] 1) arr2 = arr.each{|element| element = element * 2} #arr与arr2仍然都等于[1,2,3]   each返回原数组 ...

  2. DataTables 表格固定栏使用方法

    有时候数据过多,为了用户体验,需要将重要的栏目固定不动,如下图所示: 从上图我们可以看出,表格滚动的时候,左边5栏是不动的.现在说一下实现方法: 插件地址: https://datatables.ne ...

  3. QSignalMapper Class

    /************************************************************************************** * QT QSignal ...

  4. Spring Annotation是怎么工作的?

    最近刚好看了下注解,虽然明白了注解的作用原理,但是仍然不明白Spring中的注解是如何工作的. 占座用,留待后续. 先来两个链接吧 https://dzone.com/articles/spring- ...

  5. const 与#define 的比较

    const 与#define 的比较 C++ 语言可以用 const 来定义常量,也可以用 #define 来定义常量.但是前者比后 者有更多的优点: (1) const 常量有数据类型,而宏常量没有 ...

  6. 通过json传递图片(base64编码)

    程序一: 后台代码: public ActionResult Index() { FileStream fs = new FileStream("e:\\file\\psb.jpg" ...

  7. php下保存远程图片到本地的函数

    <?php header("content-type:text/html;charset=utf-8"); function GrabImage($url,$filename ...

  8. NSArray打印汉字的方法

    (1) NSArray打印汉字 通过重载NSArray的- (NSString *)descriptionWithLocale:(id)locale方法 方法体例如以下: //依据设置的locale ...

  9. 如何在vs2008安装64位编译器

    1.打开D:\Microsoft Visual Studio 9.0\Microsoft Visual Studio 2008 Professional Edition - CHS setup.exe ...

  10. SharePoint 2010用“localhost”方式访问网站,File not found问题处理方式

    场景:本地服务器上,用“localhost”方式访问网站:在某网站集(Site Collection)下的子网站(Sub Site)中,点击网站权限菜单(Site permissions)等关于调用L ...