使自定义UISlider缩略图区域更大?

时间:2013-08-18 08:49:03

标签: iphone ios thumbnails subclass uislider

我正在尝试创建自定义UISlider,并希望增加拇指图像的隐藏“点击区域”?下面的代码效果很好,但拇指的“点击区域”保持不变。如果我没有错,我相信标准拇指区域大约是20像素?

我现在的情况如下:滑块效果很好,看起来很棒,但我必须能够将“隐藏的点击区域”增加到我用于拇指的图片那么大。让我们说我想用下面的代码将它增加到35px,我需要采取哪些步骤?

- (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;

     }   

此代码的问题在于我没有将UISlider子类化。我已经将代码更改为以下(子类),我不确定我需要什么样的代码才能使其正常工作?

我得到以下内容:http://tinypic.com/view.php?pic=347dufa&s=5。第一张图片表示未被子类化时的情况nr 1,并且“隐藏点击区域”未按预期工作,而图片nr 2表示子类时的情况nr 2(下面的代码)。我现在想知道A),我是在正确的轨道编码吗? B)我应该在这个方法中加入什么

在我的方法中:

- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {  CGRectInset([super thumbRectForBounds:bounds trackRect:rect value:value],-10,-10); }

为了将拇指放在正确的位置,并使用更大的(隐藏的点击区域)再次滑动。

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;

}

编辑(尝试使用下面的代码进行子类化)

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

0 个答案:

没有答案