user2924482
user2924482

Reputation: 9120

Swift hitTest:point withEvent to detect subview

I'm trying to detect when subView is tap using hitTest. Here is my implementation:

class MyViews:UIView {
    override init (frame : CGRect) {
        super.init(frame : frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        for subview in self.subviews.reversed() {
            let subPoint = subview.convert(point, from:self);
            
            if let result = subview.hitTest(subPoint, with:event) {
                return result;
            }
        }
        return nil
    }
}

Here is how I'm adding the subViews:

    override func viewDidLoad() {
        super.viewDidLoad()
        let gesture = UITapGestureRecognizer(target: self, action: #selector(doSomethingHere(_:)))
        
        let blueView = MyViews(frame: CGRect(origin: CGPoint(x: 38.0, y: 231.0), size: CGSize(width: 335.0, height: 444.0)))
        blueView.tag = 100
        blueView.isUserInteractionEnabled = true
        blueView.backgroundColor = .blue
        blueView.addGestureRecognizer(gesture)
        view.addSubview(blueView)
        
        let grey = MyViews(frame: CGRect(origin: CGPoint(x: 38.0, y: 20.0 ), size: CGSize(width: 279, height: 203.0)))
        grey.tag = 200
        grey.backgroundColor = .gray
        grey.isUserInteractionEnabled = true
        grey.addGestureRecognizer(gesture)
        blueView.addSubview(grey)
        
        let white = MyViews(frame: CGRect(origin: CGPoint(x: 56.0 , y: 17.0 ), size: CGSize(width: 182.0, height: 92)))
        white.tag = 300
        white.isUserInteractionEnabled = true
        white.addGestureRecognizer(gesture)
        grey.addSubview(white)
        
        let result = blueView.hitTest(CGPoint(x: 157.5, y: 149.0), with: nil)
        print(result ?? "no result")

    }

I'm simulating a touch on this line let result = blueView.hitTest(CGPoint(x: 157.5, y: 149.0), with: nil)

But the response from this function:

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        for subview in self.subviews.reversed() {
            let subPoint = subview.convert(point, from:self);
            
            if let result = subview.hitTest(subPoint, with:event) {
                return result;
            }
        }
        return nil
    }

It always is nil. I don't understand why it should be returning the white view.

Any of you knows why this function is not returning the correct view?

I'll really appreciate your help.

Upvotes: 0

Views: 2798

Answers (1)

DPrice
DPrice

Reputation: 174

It looks like you hitTest is never checking to see if the point is in the view. Give this a whirl:

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    // if there are subviews, step through them
    // if the subview contains the point, call hitTest on the subview to see if the subview's subviews contain the point
    // if they do, return the result,
    // if they don't, return the subview that does contain the point.
    for subview in self.subviews.reversed() {
        let subPoint = subview.convert(point, from:self)
        if subview.point(inside: subPoint, with: event) == true {
            if let result = subview.hitTest(subPoint, with: event) {
                return result
            } else {
                return subview
            }
        }
    }
    // none of the subviews contain the point,(or there are no subviews) does the current view contain the point?
    // if yes, return self. otherwise, return nil
    if self.point(inside: point, with: event) == true {
        return self
    } else {
        return nil
    }
}

Upvotes: 4

Related Questions