Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch events passing to superview through transparent sections

I have a simple app which adds a subview over the main view when the user clicks on a UIButton in the main view. This subview is of size 480x320 (I'm in landscape mode), but there is a boarder around the centre image in this subview which is transparent.

This is where my problem lies. I would like only the subview to process touches until it is removed from the superview, but if there is a touch event on the transparent boarder, the event gets passed to the superview, and ignores the subview, even though the subview is the full size of the window.

Doing some research into this, it seems as though this is what apple intended to happen, as touches will only get passed to opaque sections, even if the subview is the full size of the window. It is explained in the reference:

http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html

I would like to be able to set it such that the touches will stay with the subview, regardless of any transparency issues. The hitTest:withEvent: method seems to do something like this, but more for passing touches to different subviews then the one that was touched.

Is there anyone who has a fix/work-around that can achieve this?

like image 202
msgambel Avatar asked Feb 24 '23 12:02

msgambel


2 Answers

Simple solution is to subclass your subview and add the following empty method...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}

This will prevent the touch from getting passed to the superview, even if the user were to touch a transparent part of the view.

like image 79
AlBeebe Avatar answered Feb 26 '23 02:02

AlBeebe


You should just put a clear button in the back of the xib. That will prevent touch events from going off.

like image 41
William Avatar answered Feb 26 '23 01:02

William