iOS中的触摸穿透hitTest


There is a requirement that can be scaled and moved in a common view in my project. But move the subview event, I need to implement touchesMoved:withEvent: method. So, the scale event was hijacked.This time we need to touch through.

Idea is very simple, we just need to tell the program which view to respond to events. This time we need hitTest:withEvent: method.

The function hitTest:withEvent: method is to find out what the view is under the touch point. Then according to our own logic to determine which view to respond to events.

views within views hierarchy, as follows:

Views Hierarchy

Put your finger inside D. Here's what will happen:

  1. hitTest:withEvent: is called on A, the top-most view of the view hierarchy.
  2. pointInside:withEvent: is called recursively on each view.
  3. pointInside:withEvent: is called on A, and returns YES.
  4. pointInside:withEvent: is called on B, and returns NO.
  5. pointInside:withEvent: is called on C, and returns YES.
  6. pointInside:withEvent: is called on D, and returns YES.
  7. On the views that returned YES, it will look down on the hierarchy to see the subview where the touch took place. In this case, from A, C and D, it will be D.
  8. D will be the hit-test view.

That's all.

Now, let's look at a simple demo.

The views hierarchy of storyboard, as follows:

Storyboard

The back view is my custom view, it is implementation as follows:

BackgroundView.swift

import Foundation
import UIKit

class BackgroundView: UIView {
    
    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        // Find out what the view is under the touch point 
        let hitView = super.hitTest(point, withEvent: event)
        
        // determine which view to respond to events
        if let customView = self.subviews.last as? CustomView {
            
            let movedViewPoint = customView.movedView.convertPoint(point, fromView: self)
            let moved2ViewPoint = customView.movedView2.convertPoint(point, fromView: self)
            
            if customView.movedView.pointInside(movedViewPoint, withEvent: event) {
                customView.userInteractionEnabled = true
                return customView.movedView
            } else if customView.movedView2.pointInside(moved2ViewPoint, withEvent: event) {
                customView.userInteractionEnabled = true
                return customView.movedView2
            }
        }
        
        return hitView;
    }
}

We need implement touchesMoved:withEvent: in custom view class.

CustomView.swift

import Foundation
import UIKit

class CustomView: UIView {
    
    var movedView: UIView!
    var movedView2: UIView!
    
    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        
        let hitView = super.hitTest(point, withEvent: event)
        if hitView != self.movedView {
        
            // Close user interaction, this time image view can be scaled
            self.userInteractionEnabled = false
        }
        
        return hitView
    }
    
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let allTouches = event?.allTouches() {
            if let touch = (allTouches as NSSet).anyObject() {
                let optionView: UIView? = touch.view
                if optionView != nil {
                    if optionView!.tag < 101 {
                        return
                    }
                    let point = touch.locationInView(self)
                    optionView!.center = point
                }
            }
        }
    }
    
    func addMovedViewInView() {
        self.movedView = UIView(frame: CGRectMake(self.center.x, self.center.y, 100, 100))
        self.movedView.tag = 101
        self.movedView.layer.cornerRadius = 50.0
        self.movedView.backgroundColor = UIColor.blackColor()
        self.addSubview(self.movedView)
        
        self.movedView2 = UIView(frame: CGRectMake(50, 50, 100, 100))
        self.movedView2.tag = 102
        self.movedView2.layer.cornerRadius = 50.0
        self.movedView2.backgroundColor = UIColor.orangeColor()
        self.addSubview(self.movedView2)
    }
}

ViewController.swift

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var backView: BackgroundView!
    @IBOutlet weak var scrollView: UIScrollView!
    
    var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.scrollView.delegate = self;
        self.scrollView.minimumZoomScale = 1.0;
        self.scrollView.maximumZoomScale = 3.0;
        self.scrollView.setZoomScale(self.scrollView.minimumZoomScale, animated: true)
        
        self.imageView = UIImageView(image: UIImage(named:"testImage"))
        self.imageView.frame = self.view.frame;
        self.scrollView.addSubview(self.imageView)
        
        let customView = CustomView(frame: UIScreen.mainScreen().bounds)
        customView.backgroundColor = UIColor.clearColor()
        customView.addMovedViewInView()
        self.backView.addSubview(customView)
    }
    
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return self.imageView
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

**Demo from github: **

Reference from:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容