How can I make touch events via UIView (similar to event pointers: not in CSS)?

CSS pointer-events:none;allows click events to go through an element. I am curious if there is something similar that I can do in Objective-C on iOS for UIViews.

Here's jsfiddle for example event pointers: none.

Can I somehow achieve the same behavior for UIView by overriding hitTest:withEvent: ? Or maybe there is another way to do this?

Thanks for any help.

+4
source share
4 answers

What you are looking for is usually called "Event Bubbling."

Apple : https://developer.apple.com/documentation/uikit/understanding_event_handling_responders_and_the_responder_chain

, , hitTest:withEvent:, , . .

+3

:

#import "TouchTransparentView.h"

@implementation TouchTransparentView

-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    id hitView = [super hitTest:point withEvent:event];
    if (hitView == self) {
        return nil;
    } else {
        return hitView;
    }
}

@end
+11

:

import UIKit

class PassThroughView: UIView {

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

        let hitView = super.hitTest(point, with: event)

        if hitView == self {
            return nil
        } else {
            return hitView
        }
    }
}

...

+2

-, pointer-events:none,

yourView.userInteractionEnabled = NO

.

+1

Source: https://habr.com/ru/post/1529363/


All Articles