Swift: hitTest for UIView underneath another UIView

后端 未结 4 1490
闹比i
闹比i 2021-01-06 08:48

I have TransparentUIView on top of RedOrGreenUIView. TransparentUIView has a UILongPressGestureRecognizer attached to it. Once user begins a long touch on it, I check for .C

相关标签:
4条回答
  • 2021-01-06 08:58

    Create a custom view for your container and override the pointInside: message to return NO when the point isn't within an eligible child view, like this:

    @interface PassthroughView : UIView
    @end
    
    @implementation PassthroughView
    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        for (UIView *view in self.subviews) {
            if (!view.hidden && view.alpha > 0 && view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event])
                return YES;
        }
        return NO;
    }
    @end
    

    swift version

    class PassThroughView: UIView {
    
         override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
                for subview in subviews as [UIView] {
                    if !subview.hidden && subview.alpha > 0 && subview.userInteractionEnabled && subview.pointInside(convertPoint(point, toView: subview), withEvent: event) {
                        return true
                    }
                }
                return false
            }
        }
    
    0 讨论(0)
  • 2021-01-06 09:09

    Swift 4 version :

     override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
            for subview in YourView.subviews as [UIView] {
    
                if !subview.isHidden && subview.alpha > 0 && subview.isUserInteractionEnabled && subview.point(inside:point, with: event) {
                    return true
                }
            }
    
        return false
     }
    
    0 讨论(0)
  • 2021-01-06 09:13

    If one UIView gets touches, others underneath don't get any.

    So either you can have a central class that passes touches to both your UIView objects, or the first UIView, the one that is on top, passes its UITouch object to the UIView underneath and conducts the hitTest.

    0 讨论(0)
  • 2021-01-06 09:18

    I am currently working on a transparent Help Overlay for iOS apps and was looking for a good answer on this myself.

    I couldn't really find anything, so decided to look a bit further. The apple documentation on hitTest says it doesn't include views that have a transparency of < 0.1, are hidden or have userInteractionEnabled to false.

    That last one gave me an idea and it's something that seems to work pretty good.

    Try the following (I still need to test it further, but it seems to be allright):

    var p:CGPoint = rec.locationInView(self.view)
    
    TransparentUIView.isUserInteractionEnabled = false
    
    var selectedView = view.hitTest(p, withEvent: nil)
    
    TransparentUIView.isUserInteractionEnabled = true
    
    if selectedView != nil {
        if selectedView == TransparentUIView {
            println("TransparentUIView is being touched")
        }
    }
    
    0 讨论(0)
提交回复
热议问题