以下是我解决问题的方法:
1) 子类UISlider
2) 创建了一个清晰的图像,并将滑块拇指设置为该清晰的图像。
[self setThumbImage:[UIImage imageNamed:@"ClearImageForOverride.png"] forState:UIControlStateNormal];
[self setThumbImage:[UIImage imageNamed:@"ClearImageForOverride.png"] forState:UIControlStateHighlighted];
3) 创建了一个UIImageView,并使其跟随拇指中心。
float imageWH = PassedInHeightWidthFromSubClass;
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 1, imageWH, imageWH)];
imageView.image = [UIImage imageNamed:@"NewThumbImage"];
[self addSubview:imageView];
[self bringSubviewToFront:imageView];
imageView.center = CGPointMake(thumbRect.origin.x + self.frame.origin.x + localImageViewCenter, self.frame.origin.y - sliderImageViewYOffset);
[self addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];
4) 然后,当sider值发生变化时,我会告诉imageView它的中心应该在哪里。
-(void)sliderValueChanged {
CGRect trackRect = [self trackRectForBounds:self.bounds];
CGRect thumbRect = [self thumbRectForBounds:self.bounds trackRect:trackRect value:self.value];
imageView.center = CGPointMake(thumbRect.origin.x + self.frame.origin.x + localImageViewCenter, self.frame.origin.y - sliderImageViewYOffset);
}
从本质上讲,有了这个,你可以为滑块旋钮提供任何尺寸的图像,它看起来很棒,而且可以完美地作为旋钮工作。