I am trying to create a custom UISlider and want to increase the thumb image's hidden "click area" ? The code below works great, with the exception that the thumb's "click area" remains unchanged. If I am not wrong, I believe the standard thumb area is something like 20 px large?
My situation now is the following: the slider works great, it looks great but I must be able to increase the "hidden click area" to as big as the picture I am using for the thumb. Lets say I would like to increase it to 35px with the following code below, what steps do I need to take?
- (void)create_Custom_UISlider
{
CGRect frame = CGRectMake(20.0, 320.0, 280, 0.0);
customSlider = [[UISlider alloc] initWithFrame:frame];
[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
customSlider.backgroundColor = [UIColor clearColor];
UIImage *stetchLeftTrack = [[UIImage imageNamed:@"blueslider.png"]
stretchableImageWithLeftCapWidth: 10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:@"blueslider.png"]
stretchableImageWithLeftCapWidth: 260.0 topCapHeight:0.0];
// UIImage *thumbImg = [[UIImage imageNamed:@"slider_button00.png"]
// stretchableImageWithLeftCapWidth: 100.0 topCapHeight:100.0];
[customSlider setThumbImage: [UIImage imageNamed:@"slider_button.png"] forState:UIControlStateNormal];
[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
// [customSlider thumbRectForBounds: thumb tackRect: frame value: customSlider.value];
customSlider.minimumValue = 0.0;
customSlider.maximumValue = 1.0;
customSlider.continuous = NO;
customSlider.value = 0.00;
}
The problem with this code is that I have not subclassed the UISlider. I have then changed the code to the following below (subclassed it) and I am not sure what typ of code I need to make it work properly ?
I get the following look: http://tinypic.com/view.php?pic=347dufa&s=5 . The first picture represents case nr 1 when not being subclassed and the "hidden click area" not working as intended while picture nr 2 represents case nr 2 (code below) when being subclassed. I now wonder A), am I on the right track coding like this? B) What the heck shall I put in this method
In my method:
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value { CGRectInset ([super thumbRectForBounds:bounds trackRect:rect value:value], -10 , -10); }
In order to place the thumb on the correct spot and make it slide again with a larger (hidden click area).
main.h
#import "MyUISlider.h"
@interface main : MLUIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UIActionSheetDelegate, UIAlertViewDelegate, MFMailComposeViewControllerDelegate, UIGestureRecognizerDelegate, MLSearchTaskDelegate, MLDeactivateDelegate, MLReceiptDelegate>
{
..........
}
- (void)create_Custom_UISlider;
main.m
- (void)viewDidLoad
{
[self create_Custom_UISlider];
[self.view addSubview: customSlider];
}
- (void)create_Custom_UISlider
{
CGRect frame = CGRectMake(20.0, 320.0, 280, 0.0);
customSlider = [[MyUISlider alloc] initWithFrame:frame];
[customSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
// in case the parent view draws with a custom color or gradient, use a transparent color
customSlider.backgroundColor = [UIColor clearColor];
UIImage *stetchLeftTrack = [[UIImage imageNamed:@"blue_slider08.png"]
stretchableImageWithLeftCapWidth: 10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:@"blue_slider08.png"]
stretchableImageWithLeftCapWidth: 260.0 topCapHeight:0.0];
[customSlider setThumbImage: [UIImage imageNamed:@"slider_button00"] forState:UIControlStateNormal];
[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
customSlider.minimumValue = 0.0;
customSlider.maximumValue = 1.0;
customSlider.continuous = NO;
customSlider.value = 0.00;
}
EDIT (trying to subclass with the code below)
MyUISlider.h
#import <UIKit/UIKit.h>
@interface MyUISlider : UISlider {
}
@end
MyUISlider.m
#import "MyUISlider.h"
@implementation MyUISlider
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
{
CGRectInset ([super thumbRectForBounds:bounds trackRect:rect value:value], -10 , -10);
}
@end