我知道你如何拥有最小的轨道和最大的轨道图像,以及它如何可以伸缩。但是,根据我的需要,这可能不够控制。
我有一种情况,在左侧(最小轨道),我需要根据数据显示两种不同的颜色。左侧实际上代表两个数据,但是在最小值和最大值之间仍然只存在一个拇指。
那怎么能这样做?
任何示例代码?
我其实想要这样
在左侧,它是咧嘴笑的颜色,但当它超过拇指图像时,它变成红色..
在你的.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的图像,它在条形图上滑动。
将图像交换代码放在滑块值读取方法中,通常是:
-(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)