Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I forward touches to a UIButton from another UIView?

I have a UIButton and a UIView. The View sits above the button, and is larger than the button in size. The view itself is what I want to have accept the touch events, and I'd like to forward them to the button, so that all the normal button visual changes on touch happen.

I can't seem to make this work-- my UIView implements touchesBegan/Moved/Ended/Cancelled, and turns around and calls the same methods on the button with the same arguments. But the button doesn't respond.

I have ensured that the touch methods are in fact being called on the UIView.

Is there something obvious I'm missing here, or a better way of getting the control messages across? Or is there a good reason why this shouldn't work?

Thanks!

[Note: I'm not looking for workarounds for the view+button design. Let's assume that that's a requirement. I'm interested in the notion of controls that are touch proxies for other controls. Thanks.]

like image 513
Ben Zotto Avatar asked Jan 22 '23 14:01

Ben Zotto


1 Answers

Create a ContainerView that contains a button and override hitTest:withEvent: so that it returns the UIButton instance.

like image 72
falconcreek Avatar answered Feb 11 '23 02:02

falconcreek