PlateReverb PlateReverb - 12 days ago 9
iOS Question

How Do I Trigger a Button By Sliding Onto It?

I'm working on an app with a musical keyboard component.

I need 2 types of "sent events" to trigger the keys of the keyboard (UIButtons).

1) "Touch Down" triggers the buttons they way I need it to

2) The 2nd way I need buttons to be triggered is by sliding onto a button,from another button/key to the side of it as if it is "touched down" upon, when it is slid upon from the left or right.

How do I achieve this?

Answer

You can't do this using the built-in control events of the buttons, for the simple reason that you don't get an event in a button at all unless the touch is initially in that button (as I explain here: http://stackoverflow.com/a/40414929/341994).

Still, this doesn't sound very hard to do. The simplest approach is probably to put the touch response (such as a gesture recognizer) into the common superview of all the buttons. The superview can then track the gesture. And it can very easily find out which button the touch is currently inside at any given moment. So it can manage the whole interaction. It can even send messages to the buttons telling them when to highlight and unhighlight. (And if you aren't going to use the button touch handling for anything, you might even want to give up the idea that these are buttons; they could just be views or custom controls that look like buttons.)