In CSS, pointer-events:none;
allows click events to pass through an element. I'm curious if there is anything similar that I can do in Objective-C on iOS for UIView
s.
Here's a jsfiddle to an example of pointer-events: none.
Could I somehow achieve the same behavior for a UIView
by overriding hitTest:withEvent:
? Or maybe there is another way to do this?
Thanks for any help.
Here's a handy implementation in line with your initial intuition:
#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
Swift version, with a @IB upgrade
import UIKit
@IBDesignable
class PassthroughTouchView: UIView {
@IBInspectable var passthroughTouchEvents : Bool = false
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, with: event)
if hitView == self && passthroughTouchEvents == true {
return nil;
} else {
return hitView;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With