lmirosevic
lmirosevic

Reputation: 16317

Best way to extend UIControl's hit region?

If you notice in the iPod/Music app, when you tap the progress slider, you can tap quite far off form the little knob and it will still register as if you'd tapped it. Same goes for buttons in a UINavigationController's toolbar, notice how (if there is nothing nearby) you can press quite far away from e.g. the back button and it will still register.

How do I best achieve that for my own controls? I'm actually doing it on a UIProgress slider.

Upvotes: 1

Views: 839

Answers (1)

Nick Lockwood
Nick Lockwood

Reputation: 41005

There is a complex approach that involves overriding the hitTest method on a custom UIControl subclass, but the simplest solution is just to make the control larger than the visible size on screen.

For example, for a button you might make the button itself 50x50 pixels, but make the button graphic 20x20 pixels, so it looks quite small but has a large hit area.

The same technique can work for your slider. Make the UISlider frame quite tall (say 50px) but have the slider graphics be smaller. If that doesn't work very well because the control stretches them to fit, just make the graphics of the slider itself transparent and draw the thinner slider in an image view behind the real slider control.

Upvotes: 2

Related Questions