iOS 事件传递笔记

来自:https://www.jianshu.com/p/481465fc4f2d

 

事件分为3种:屏幕的、传感器的和外接设备。

 

事件触发时候的步骤分为:传递、响应。

 

传递:  从视图的层级root 到 最前面的的View 使用的的func为hittest,这个函数的意思就是检查当前的触摸点是不是在当前视图中。

是:

  将触摸点转换为子视图在该视图的坐标系中,继续如上的试探。直到找到一个,立马停止接下来的试探。(子视图的寻找顺序是从 Index大的向小的进行)

否:结束在该View的事件传递,有则转为下一个同级View

 

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.hidden || !self.userInteractionEnabled || self.alpha < 0.01 || ![self pointInside:point withEvent:event] || ![self _isAnimatedUserInteractionEnabled]) {
        return nil;
    } else {
        for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
            UIView *hitView = [subview hitTest:[subview convertPoint:point fromView:self] withEvent:event];
            if (hitView) {
                return hitView;
            }
        }
        return self;
    }
}

可以看到,最终没有子View的时候,返回的就是这个最顶层的View。

 

响应:

  在得到这个事件传递的路径之后。就会由子向父传递。直到该View添加了响应手势的办法,或者实现如下方法:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

  事件就会在这里响应,停止向上传递。

 

新增事件之间的区别:

  手势识别和View的接受触摸事件。如果手势识别成功,那么取消事件的继续传递。

  target-action的 消息来响应。消息不是事件,不通过Responder Chain派发,不会走响应者链。 有一种情况:如果消息没有target,那么,会走响应者链,比如 editing menu的 cut: copy:等。

  ps:操作就是 事件传递完毕,检查直到当前第一个实现了UIResponse的View,此时会调用touchBegin;接着再向后查找是否有手势;有的话就touchesCanceled,touch事件取消。手势响应;

    只不过这并不包含UIControl子类以及UIGestureRecognizer的子类,这两类会直接打断响应者链。

 

 

简单的栗子:

//
//  ViewController.swift
//  tableViewTest
//
//  Created by StarSky_MacBook Pro on 2019/9/23.
//  Copyright © 2019 StarSky_MacBook Pro. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellid = "testCellID"
        var cell = tableView.dequeueReusableCell(withIdentifier: cellid)
        if cell==nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellid)
        }
        
        cell?.textLabel?.text = "这个是标题~"
        cell?.detailTextLabel?.text = "这里是内容了油~"
        cell?.imageView?.image = UIImage(named:"Expense_success")
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("我选择了%@",indexPath.row)
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let tableView = UITableView(frame: view.bounds, style: .grouped)
        tableView.backgroundColor = UIColor.white;
        view.addSubview(tableView)
        tableView.dataSource = self
        tableView.delegate = self
        
        let tableViewClick = UITapGestureRecognizer(target: self, action: #selector(tvClickAction))
        tableView.addGestureRecognizer(tableViewClick)
        //开启 isUserInteractionEnabled 手势否则点击事件会没有反应
        tableViewClick.delegate = self
        tableView.isUserInteractionEnabled = true

    }
    //点击事件方法
    @objc func tvClickAction() -> Void {
        print("tableView被点击")
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        
        print("hello")
        
        if (NSStringFromClass(touch.view!.classForCoder) == "UITableViewCellContentView") {
            //判断如果点击的是tableView的cell,就把手势给关闭了
            self.tvClickAction()
            return false;
            
            //关闭手势
            
        }
        
        //否则手势存在
        
        return true;
    }

}

 

image = image[int(tongue_loc[1] - 0.5 * tongue_loc[3]):int(tongue_loc[1] + 0.5 * tongue_loc[3]),
                      int(tongue_loc[0] - 0.5 * tongue_loc[2]):int(tongue_loc[0] + 0.5 * tongue_loc[2])]

 

posted @ 2019-09-24 09:09  事不过三寻  阅读(276)  评论(0编辑  收藏  举报