Receiving touch events on more then one UIView simultaneously

若如初见. 提交于 2019-12-18 13:38:25

问题


I have a bunch of UIViews stacked one upon the other(not nested). I want them all to react to touch, but it seems that the topmost view obscures the views beneath it, preventing them from receiving touch events.

At first i thought i’d catch all touch events with the topmost view, and then manually call
hitTest, or pointInside methods on all the underlying views, but i found out that both methods are somehow private(could it be?) and cannot be accessed.

Any ideas how to pull it off?


回答1:


You can check if the touch is for your topmost view. If it doesn't you can call the same method of your superview. Something like [self.superview sameMethod:sameParameter].

Your topmost view has a method

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

Inside that method you are doing your logic right? Inside the method can't you check if the touch received is at your topmost view with

UITouch *touch = [touches anyObject];
[touch locationInView:self];

And if it doesn't you pass it to the superView's same method using

[self.superview touchesEnded:touches withEvent:event];



回答2:


Touches are sent to a single view. That view can then optionally pass them up the responder chain. If you want to handle touches to a collection of views you should have them forward those events up to the next responder and have a common parent of all of them (or their view controller since the controller is also part of the responder chain) handle those touches.

https://developer.apple.com/library/mac/documentation/General/Devpedia-CocoaApp-MOSX/Responder.html



来源:https://stackoverflow.com/questions/4585007/receiving-touch-events-on-more-then-one-uiview-simultaneously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!