UISlider Thumb Image Pixelated

时间:2013-09-26 03:00:35

标签: uiimageview uiimage uicontrol

所以我有一个UISlider,我一直在努力,我需要它有一个自定义图像。我有一个很好的@ 2x图像,它具有大量的像素,当我将它添加到UIImageView时看起来很棒,但是当我使用相同的图像替换UISlider拇指图像时,它会像素化它使滑块的拇指看起来像垃圾。关于如何解决这个问题的任何想法?

以下是我的一些示例代码。

 slider = [[UISlider alloc] initWithFrame:CGRectMake(sliderOffset, 29.5, siderW, 50)];
    [slider addTarget:self action:@selector(sliderAction) forControlEvents:UIControlEventValueChanged];
    [slider setBackgroundColor:[UIColor clearColor];
    slider.minimumValue = 0.0;
    slider.maximumValue = 10000.0;
    slider.continuous = YES;
    slider.value = 3000.00;
    [slider setThumbImage:[UIImage imageNamed:@"Slider@2x.png"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"Slider@2x.png"] forState:UIControlStateHighlighted];

提前致谢!

1 个答案:

答案 0 :(得分:2)

以下是我如何解决它:

1)Subclassed 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);
}

基本上,你可以使用滑块旋钮的任何尺寸的图像,它看起来很棒并且作为旋钮完美无瑕地工作。