Passing a touch event to all subviews of a View Controller

被刻印的时光 ゝ 提交于 2019-12-05 02:10:10

问题


I have a view controller which creates multiple subviews on top of it. All subviews and the view controller accept touches. How can i communicate the touch point information to all the subviews on the screen? Keep in mind that each subview covers the entire screen so after a few additions its a bit like pages in a book. That is, any subviews below the top subview can't be touched directly by the user. I have tried using [self.nextResponder touchesBegan:touches withEvent:event] however this only sends the touch information from the top subview to the superview bypassing all other subviews on the screen. Thanks


回答1:


I would put another subview ontop of everything. This would grab all the touches that you were interested in and pass them back to the view controller. Your view controller would then iterate through all it's child UIViews, giving them the events.

i.e. something like

for (UIView *view in subviews)
  [view touchesBegan:touches withEvent:event];

though that's assuming that all the subviews you want to pass events to are children of your view controller.




回答2:


Imlement your touch method only in the superview. From there iterate over all subviews and send them the touch method.




回答3:


Alternatively you can send NSNotification to all the subviews. And in the subvews class you can have a better control and can handle the notifications to perform some task.



来源:https://stackoverflow.com/questions/1402614/passing-a-touch-event-to-all-subviews-of-a-view-controller

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