简体   繁体   English

自定义 UISlider 无法正确设置最小轨道颜色

[英]Custom UISlider Not able to set the minimum track color properly

I've subclassed UISlider and increased the width by doing this:我已经继承了 UISlider 并通过这样做增加了宽度:

-(CGRect)trackRectForBounds:(CGRect)bounds {

bounds.size.height = 50;
return bounds;

}

And changed the thumb image:并更改了拇指图像:

- (void)drawRect:(CGRect)rect {
// Drawing code
[self setThumbImage:[UIImage imageNamed:@"icon_circle"] forState:UIControlStateNormal];
}

But this is happening:但这正在发生:

在此处输入图片说明 在此处输入图片说明

The minimum track image starts from the center.最小轨道图像从中心开始。 I want to make it start from the thumb end.我想让它从拇指末端开始。 So initially, till the thumb it should be blue and when I drag it till the end the whole slider should be blue but at the end of the slider, the slider is not curved.所以最初,直到拇指它应该是蓝色的,当我把它拖到最后时,整个滑块应该是蓝色的,但在滑块的末端,滑块没有弯曲。 It has got sharped edges.它有锋利的边缘。 What should I do to get this right?我该怎么做才能做到这一点?

Couldn't find much help so I made my own custom one instead.找不到太多帮助,所以我自己定制了一个。 It's also available on GitHub.它也可以在 GitHub 上找到。 https://github.com/bhavukjain1/SlideToActionSlideriOS https://github.com/bhavukjain1/SlideToActionSlideriOS

在此处输入图片说明

Are you setting the following properties?您是否正在设置以下属性?

UIImage *minImage = [[UIImage imageNamed:@"slider_minimum.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
UIImage *maxImage = [[UIImage imageNamed:@"slider_maximum.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
UIImage *thumbImage = [UIImage imageNamed:@"sliderhandle.png"];

[[UISlider appearance] setMaximumTrackImage:maxImage forState:UIControlStateNormal];
[[UISlider appearance] setMinimumTrackImage:minImage forState:UIControlStateNormal];
[[UISlider appearance] setThumbImage:thumbImage forState:UIControlStateNormal];

} }

if so, take a look at this tutorial of how to create a custom slider, it comes with a sample project, may be it's easier for you to find your error by comparing your implementation against a working one.如果是这样,请查看有关如何创建自定义滑块的 教程,它附带一个示例项目,通过将您的实现与工作中的实现进行比较,您可能更容易找到错误。

To solve the rectangular corners issue, in drawRect为了解决矩形角问题,在 drawRect 中

self.layer.cornerRadius = 25;
self.clipsToBounds = YES;

To show the correct image while dragging, you need to set this as well:要在拖动时显示正确的图像,您还需要设置:

[self setThumbImage:[UIImage imageNamed:@"icon_circle"] forState:UIControlStateHighlighted];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM