iPhone:编程 UISlider 以定位在单击的位置
iPhone编程中如何在UISlider上设置滑块到点击位置并获取点击位置的滑块值。 我知道我们可以将滑块拖动到该位置,但我不想这样做。您能告诉我如何将滑块设置到单击位置吗?这可以吗?
How to set the slider to clicked position and get the slider value on the clicked location on UISlider in iPhone programming.
i know we can drag the slider to that position but i dont want to do it. Can you please tel me how to set the slider to clicked position? Is this possible to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是“留给用户练习”的部分:
Here is the part "left as a user exercise":
我的方法是对滑块进行子类化并签入
touchesBegan
。如果用户点击拇指按钮区域(我们跟踪的区域),则忽略该点击,但我们会忽略轨迹栏上的任何其他位置::
The way I did it is to subclass the slider and check in
touchesBegan
. If the user taps on the thumb button area (which we track) then ignore the tap, but any where else on the trackbar we do::
您只需将 UITapGestureRecognizer 添加到滑块,然后拉出与其关联的 UIEvent 和 Touches,即可确定点击沿 UISlider 发生的位置。然后将滑块的值设置为该计算值。
更新:
首先,设置滑块并向其添加手势识别器。
然后实现选择器
*提示: 阅读文档< /a> 找出如何推断 locationInView 并找出滑块应该是什么
You can simply add a UITapGestureRecognizer to the slider, then pull out the UIEvent and Touches associated with it to figure out where along the UISlider the tap took place. Then set your slider's value to this calculated value.
UPDATE:
First, setup your slider and add the gesture recognizer to it.
Then implement the selector
*Hint: Read the docs to figure out how to get the locationInView extrapolated and figure out what the slider should be
似乎只是子类化 UISlider 并始终返回 true 到 beginTracking 就可以产生所需的效果。
iOS 10 和 Swift 3
It seems like just subclassing UISlider and returning always true to the beginTracking produce the desired effect.
iOS 10 and Swift 3
我正在使用子类 UISlider 代码来监听 ios6> 中的 ValueChanged 事件为了实现捕捉到网格类型的功能。
当升级到 iOS7 时,这个问题被打破了,因为它不再触发 UIControlEventsValueChanged。对于遇到相同问题的任何人,可以通过在 if() 中添加一行来解决,如下所示:
希望它可以帮助某人:)
I was using the subclassed UISlider code to listen to ValueChanged events in ios6> in order to implement a snap-to-grid type of function.
This was broken when upgrading to iOS7 as it no longer fired the UIControlEventsValueChanged. For anyone having the same problem it is fixed by adding a line in the if() as below:
Hope it helps someone :)
我已经使用过这段代码。获取自:http://imagineric.ericd.net /2012/11/15/uislider-touch-to-set-value/
希望这可以帮助您。
I have used this code. Get from: http://imagineric.ericd.net/2012/11/15/uislider-touch-to-set-value/
Hope this can help you.