如何让 UISlider 响应拇指滑块上方和下方轨道上的点击事件
我有一个 284 像素宽的 UISlider,范围为 0-25。用户需要选择一个精确到小数点后 1 位的值(例如 12.3),这可以通过滚动手指来小心地实现,但是很繁琐,并且当您抬起手指时往往会改变值。
我正在尝试对其进行安排,以便您可以通过点击拇指上方或下方的滑块将滑块向上或向下移动 0.1 个间隔。我确信这是可能的,但由于这是我的第一个 xcode 项目,我正在努力。有人能指出我正确的方向吗?
I have a 284 pixel wide UISlider with a range of 0-25. The user needs to select a value to 1 decimal place (say 12.3), which is possible with care by rolling your finger, but is fiddly and tends to change value as you lift your finger.
I am trying to arrange it so that you can inch the slider up or down by 0.1 intervals by tapping on the slider rail above or below the thumb. I'm sure it's possible, but as this is my first xcode project I'm struggling. Can anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
重新实现。
我认为更好的解决方案就是子类化 UISlider,并在触摸事件位于滑块边界内时返回 YES 来
I think a better solution is just to subclass UISlider, and to re-implement
by returning YES when the touch event is inside the slider bounds.
iPhone OS 的 UISlider 控件的一个长期存在的问题是你无法将其变得更高。更具体地说,您可以通过为其拇指和轨道指定更高的图像来使其看起来更高,但触摸区域的高度仍为 23 像素。
您需要做的是子类 UISlider,并覆盖类中的 pointInside:
在 Interface Builder 中,将滑块设置为使用新的 UISlider 子类,现在您拥有一个可触摸高度为 43px 的滑块。
A long-standing problem with the iPhone OS' UISlider control is that you can't make it taller. More specifically, you can make it look taller by specifying taller images for its thumb and track, but the touch area stays as a tiny 23px tall.
What you need to do is subclass UISlider, and override pointInside in your class:
In Interface Builder, set the slider to use your new UISlider subclass, and you now have a slider with a 43px touchable height.
我想到了几个选项:
我不确定创建一个非标准(尤其是不可见的!)实现是一个好主意,但是 - 我建议您添加可见按钮进行微调。侧面的简单向上箭头/向下箭头可能会有所帮助。还要考虑 UISlider 是否是您想要的,因为它对您来说工作得不够好。
A couple options come to mind:
I'm not sure that creating a non standard (especially invisible!) implementation is a great idea, however - I'd recommend you add visible buttons for fine-tuning. A simple up-arrow / down arrow to the side might help. Also consider if a UISlider is what you want at all, given that it doesn't work well enough for you.
在 Interface Builder 中,您无法设置 UISlider 的高度,因为它是固定的。但是在您设置拇指图像和轨道图像值的代码中,您可以设置帧高度,如下所示。这将打开对触摸敏感的区域。
In the Interface Builder you cannot set the height of UISlider because it is fixed. But in your code where you set the values for the thumb image and the track images you can set the frame height as you see below. This will open up the area which is sensitive to touches.