开发者

Touch events passing to superview through transparent sections

开发者 https://www.devze.com 2023-03-21 13:28 出处:网络
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

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?


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.


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


If you make the backgroundColor of your subview hidden, you will obtain the same effect (a transparent border around the UIButton), and your view will receive touches:

You can execute this code just before adding the subview:

subview.backgroundColor = [UIColor clearColor];
subview.hidden = NO;
[parentView addSubview:subview];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号