Pages

Subscribe:

Monday, October 10, 2011

More sensitive or responsive custom UISlider





Normally a slider contains a limited area to detect the touch & act on that. Default height of a
slider is 23 pixels, in which the thumb contains only 19 pixels area to get the touch.


Sometimes we need to use a slider which should act more smooth & responsive just like the native "Slide to unlock" button of iPhone. So, for that, we need to increase the area of touch of a slider.


To make a slider with increased touch area, at first you need to make a subclass of UISlider, such as MySlider.

So MySlider.h file will look like this:

#import

@interface MySliderClass : UISlider {

}

@end


MySlider.m file will look like this:

#import "MySliderClass.h"

/* How many extra touchable pixels you want above and below
the 23px (default height) slider */

#define SIZE_EXTENSION_Y -10 // Adjust this value as required in your project

// Define another value if you want to extend size in x-axis

@implementation MySliderClass

- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent*)event {

NSLog(@"pointSide");

CGRect bounds = self.bounds;

bounds = CGRectInset(bounds, 0, SIZE_EXTENSION_Y);

return CGRectContainsPoint(bounds, point);

}

@end

Now in your interface builder, set the slider to use your new MySlider subclass, and you now have a slider with a 43px touchable height.

1 comment:

Unknown said...

Stupid question:
How do i set the slider in my interface builder to use my new class?
Can i do this in an existing project or do I have to create everything from scratch?
Can I use this function to replace the circle with an image i like?

Post a Comment