如何创建滑块?

时间:2011-08-30 07:27:38

标签: iphone objective-c xcode user-interface

如何在iPhone中的自定义滑块中创建滑块。目前我正在使用从“xib”(界面生成器)中选择的滑块,但它显示我正常的滑块。但我需要使用我自己的按钮自定义滑块并显示文本

  

滑动开始

我该怎么做才能帮助我。

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

你可以像

那样完成
UISlider *mySlider;
mySlider = [[UISlider alloc]init];
/* Slider Customization */
[mySlider setThumbImage:[UIImage imageNamed:@"yourimage.png"] forState:UIControlStateNormal];
/*the above line will sets a custom image for your slider ball i.e; thumb image*/

/* the below code will make your slider line custom */
UIImage *sliderLeftTrackImage = [[UIImage imageNamed: @"lineImage.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
UIImage *sliderRightTrackImage = [[UIImage imageNamed: @"lineImage.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];

[mySlider setMinimumTrackImage: sliderLeftTrackImage forState: UIControlStateNormal];
[mySlider setMaximumTrackImage: sliderRightTrackImage forState: UIControlStateNormal];
/* end of Slider Customization */
[mySlider release];

答案 2 :(得分:0)

您需要使用可伸缩图像进行自定义,并使用以下方法加载它们:

setMinimumTrackImage:forState:
setMaximumTrackImage:forState:
setThumbImage:forState:

最小轨迹图像是拇指左侧的部分(默认为蓝色),最大轨迹图像位于右侧。

以下是自定义的示例:

UIImage *minimumTrackImage = [UIImage imageNamed:@"sliderMin.png"];
UIImage *stretchableMinimumTrackImage = [minimumTrackImage stretchableImageWithLeftCapWidth:6 topCapHeight:6];
[slider setMinimumTrackImage:stretchableMinimumTrackImage forState:UIControlStateNormal];

UIImage *maximumTrackImage = [UIImage imageNamed:@"sliderMax.png"];
UIImage *stretchableMaximumTrackImage = [maximumTrackImage stretchableImageWithLeftCapWidth:6 topCapHeight:6];
[slider setMaximumTrackImage:stretchableMaximumTrackImage forState:UIControlStateNormal];

UIImage *handle = [UIImage imageNamed:@"sliderHandle.png"];

[slider setThumbImage:handle forState:UIControlStateNormal];

答案 3 :(得分:0)

UISlider *objslider = [[UISlider alloc] initWithFrame:CGRectMake(14, 249, 290, 22)];

UIImage *stetchLeftTrack = [[UIImage imageNamed:@"slider_fill.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];

UIImage *stetchRightTrack = [[UIImage imageNamed:@"slider.png"]
                                 stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];

[objslider setThumbImage: [UIImage imageNamed:@"slider_ball.png"]forState:UIControlStateNormal];

[objslider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];

[objslider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];

objslider.minimumValue = 0.0;

objslider.maximumValue = 100.0;

objslider.continuous = YES;

objslider.value = 0.0;

[self.view addSubview:objslider];