[英]How to Customize UISlider?
我了解您如何才能擁有最小軌道和最大軌道圖像,以及如何將其伸展。 但是,對於我的需求,這可能還不夠控制。
我有一種情況,在左側(最小軌道),我需要根據數據顯示兩種不同的顏色。 左側實際上代表兩個數據,但是在最大值和最小值之間仍然只有一個拇指。
所以我該怎么辦???
任何示例代碼?
我實際上想要這樣
在左側,它具有粗糙的顏色,但是當它超過拇指圖像時,它會變成紅色。
在您的.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是充當條形圖和在條形圖上滑動的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 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.