我了解您如何拥有最小轨道和最大轨道图像,以及它如何可拉伸。但是,对于我的需要,这可能还不够控制。
我有一种情况,在左侧(最小轨道)我需要根据数据显示两种不同的颜色。左边实际上代表了两条数据,但在最小值和最大值之间仍然只有一个拇指。
那我该怎么做???
任何示例代码?
我其实想要这样
在左侧,它是咧嘴的颜色,但是当它超过拇指图像时,它会变成红色。
在您的 .h 文件中
IBOutlet UISlider *slide;
在你的 .m 文件中
UIImage *minImage = [UIImage imageNamed:@"unselected.png"];
UIImage *maxImage = [UIImage imageNamed:@"selected.png"];
UIImage *tumbImage= [UIImage imageNamed:@"thumb.png"];
minImage=[minImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
maxImage=[maxImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[slide setMinimumTrackImage:minImage forState:UIControlStateNormal];
unselected.png 和 selected.png 是充当 bar 和 thumb.png 的图像,它们在 bar 上滑动。
将图像交换代码放入滑块值读取方法中,通常是:
-(IBAction)sliderChanged:(id)sender; {}
每当您的滑块值达到某个预定义值时,将您的自定义绿色图像与自定义红色图像交换。请参见下面的示例。
// Switches the -thumbImage between an ivar named highImage and lowImage
// when the slider passes the halfway point
if (sliderValue > 0.5) {
[self updateSliderThumbWithImage:self.highImage];
} else {
[self updateSliderThumbWithImage:self.lowImage];
}
像这样定义滑块图像更新方法:
-(void)updateSliderThumbWithImage:(UIImage *)image;
{
[self.slider setThumbImage:image forState:UIControlStateNormal];
[self.slider setThumbImage:image forState:UIControlStateHighlighted];
}
// You have to set thumb images for both states
// or your image will vanish when user slides it.
// Not sure if you have to do the same with the track image, though.
希望这可以帮助某人。
我不确定我是否可以可视化您需要的东西(您有机会发布草图吗?),但是搜索 UISlider,您可以找到一些非常有创意的用途。否则,您可以创建自己的自定义 UIControl。
(来源:texlege.com)