xcode 为 iPhone 创建自定义滑块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11556548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:56:38  来源:igfitidea点击:

Creating a custom slider for iPhone

iphoneobjective-ciosxcode

提问by chwi

I need to create a custom vertical slider for my iPhone app, simply because the UISlider is to narrow. It is going to be used as a throttle "stick" for my remote controlled helicopter. I managed to rotate the slider in xcode with

我需要为我的 iPhone 应用程序创建一个自定义的垂直滑块,仅仅是因为 UISlider 变窄了。它将用作我的遥控直升机的油门“操纵杆”。我设法在 xcode 中旋转滑块

throttleSlider.transform = CGAffineTransformMakeRotation(3*M_PI_2);

but I think this is quite ugly since the slider still appear horizontally on my storyboard. Also, I can not find anyway to make the slider wider.

但我认为这很丑陋,因为滑块仍然水平出现在我的故事板上。另外,我无论如何都找不到使滑块变宽的方法。

How will I approach this problem?

我将如何解决这个问题?

Thank you.

谢谢你。

回答by Nate

I've used this implementation of UICustomSwitchbefore, from Catamount Software's blog:

我以前使用过这个实现UICustomSwitch,来自 Catamount Software 的博客:

UICustomSwitch

自定义开关

It attempts to customize the familiar UISwitchclass, but it accomplishes this by subclassing UISlider, and replacing the normal switch imagery with custom images. You could download this code, then just replace the included images with something that you draw yourself.

它尝试自定义熟悉的UISwitch类,但它通过子类化UISlider并用自定义图像替换普通开关图像来实现这一点。你可以下载这个代码,然后用你自己画的东西替换包含的图像。

Because this was intended to be a switch, which only has on and off settings, you'll see that it responds to touches with this:

因为这是一个开关,它只有开和关设置,你会看到它对触摸的响应是这样的:

[self setOn:on animated:YES];

Just get rid of the code that calls setOn:animated:so that the slider doesn't force its value all the way to 0.0 or 1.0.

只需去掉调用的代码,setOn:animated:滑块就不会将其值一直强制为 0.0 或 1.0。

回答by The dude

in the application I am developing right now we needed a custom slider. It had to be wider (44pixels wide, the default size of a table view cell). We accomplished our goal by using stretchable images and adjusting the properties of the slider:

在我现在正在开发的应用程序中,我们需要一个自定义滑块。它必须更宽(44 像素宽,表格视图单元格的默认大小)。我们通过使用可拉伸图像和调整滑块的属性来实现我们的目标:

UIImage* sliderCenterImage = [UIImage imageNamed:@"sliderCenter.png"];
[slider setThumbImage:sliderCenterImage forState:UIControlStateNormal];
UIImage *leftStretch2 = [[UIImage imageNamed:@"sliderLeft.png"]
                             stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
slider setMinimumTrackImage:leftStretch2 forState:UIControlStateNormal];
UIImage *rightStretch = [[UIImage imageNamed:@"sliderRight.png"]
                          stretchableImageWithLeftCapWidth:1.0 topCapHeight:0.0];
[slider setMaximumTrackImage:rightStretch2 forState:UIControlStateNormal];

This way I got an slider that fulfills a whole cell of a tableView. thumbImage is the image that represents the middle of the slider (the one you interact with, by default a round dot), minimumTrackImage will be the part on the left of the slider (the one that by default is blue), and maximumTrackImage is the part usually left blank on the right of the center of the slider. There are more properties of the UISlider that allow further customization. Take a look at UISlider developer reference

通过这种方式,我得到了一个可以填充 tableView 的整个单元格的滑块。thumbImage 是代表滑块中间的图像(你与之交互的那个,默认是一个圆点), minimumTrackImage 将是滑块左边的部分(默认是蓝色的),而maximumTrackImage 是部分通常在滑块中心的右侧留空。UISlider 的更多属性允许进一步自定义。看一看UISlider 开发者参考

Hope this helps.

希望这可以帮助。

回答by The4thIceman

As an updated answer, stretchableImageWithLeftCapWidth is deprecated. It is advised to use

作为更新的答案,stretchableImageWithLeftCapWidth 已弃用。建议使用

resizableImageWithCapInsets

resizableImageWithCapInsets

For a custom slider in swift:

对于 swift 中的自定义滑块:

    var thumb = UIImage(named: "slider_thumb")
    musicSlider.setThumbImage(thumb, forState: UIControlState.Normal)

    var left = UIImage(named: "left_slide")?.resizableImageWithCapInsets(UIEdgeInsets(top: 0.0,left: 0.0,bottom: 0.0,right: 0.0))
    musicSlider.setMinimumTrackImage(left, forState: UIControlState.Normal)
    var right = UIImage(named: "right_slide")?.resizableImageWithCapInsets(UIEdgeInsets(top: 0.0,left: 0.0,bottom: 0.0,right: 0.0))
    musicSlider.setMaximumTrackImage(right, forState: UIControlState.Normal)

The UIEdgeInserts specify the buffer of the image that won't get resized. so left:15.0 would mean the area 15 pixels in from the left will not get resized. In my case I didn't care so it was all zero.

UIEdgeInserts 指定不会调整大小的图像缓冲区。所以 left:15.0 意味着距离左边 15 个像素的区域不会被调整大小。在我的情况下,我不在乎所以它都是零。

As a note, the images themselves are what is making the slider bar wider. My thumb image is 35 pixels in height, my left and right slider images are 20 pixels in height. All are sliced and resizable from Images.xcassets (where images should be kept)

请注意,图像本身是使滑块变宽的原因。我的拇指图像高度为 35 像素,我的左右滑块图像高度为 20 像素。所有都从 Images.xcassets 切片和调整大小(图像应该保存在那里)