如何获取UISlider拇指图像的中心
我正在创建一个自定义 UISlider 来测试一些界面创意。主要是基于使拇指图像更大。
我找到了如何做到这一点,像这样:
UIImage *thumb = [UIImage imageNamed:@"newThumbImage_64px.png"];
[self.slider setThumbImage:thumb forState:UIControlStateNormal];
[self.slider setThumbImage:thumb forState:UIControlStateHighlighted];
[thumb release];
为了计算相关值,我需要知道拇指图像在被操纵时的中心点落在哪里。该点应该位于它的超级视图的坐标中。
查看 UISlider 文档,我没有看到任何跟踪此问题的属性。
是否有一些简单的方法来计算这个值,或者可以从一些现有值中得出它?
I'm creating a custom UISlider to test out some interface ideas. Mostly based around making the thumb image larger.
I found out how to do that, like so:
UIImage *thumb = [UIImage imageNamed:@"newThumbImage_64px.png"];
[self.slider setThumbImage:thumb forState:UIControlStateNormal];
[self.slider setThumbImage:thumb forState:UIControlStateHighlighted];
[thumb release];
To calculate a related value I need to know where the center point of the thumb image falls when it's being manipulated. And the point should be in it's superview's coordinates.
Looking at the UISlider docs, I didn't see any property that tracked this.
Is there some easy way to calculate this or can it be derived from some existing value(s)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这将返回视图坐标中 UISlider 拇指图像中心的正确 X 位置:
将其放入视图控制器中并像这样使用它:(假设名为 slider 的属性)
This will return the correct X position of center of thumb image of UISlider in view coordinates:
Put it in your view controller and use it like this: (assumes property named slider)
我在阅读上述建议后尝试了这一点 -
对于 Swift 版本,
我可以通过使用此代码片段获得最准确的值。
I tried this after reading the above suggestion -
For Swift version
I could get most accurate value by using this snippet.
雨燕3
Swift 3
最好使用
-[UIView ConvertRect:fromView:]
方法代替。没有任何复杂的计算,它更干净、更容易:It's better to use
-[UIView convertRect:fromView:]
method instead. It's cleaner and easier without any complicated calculations:我想知道为什么你们都没有提供最简单的答案,包括阅读手册。您可以准确地计算所有这些值,并确保它们保持这种状态,只需使用这些方法即可:
您可以在开发人员文档中轻松找到这些方法。
如果缩略图发生变化并且您想要更改其定位方式,则可以子类化并重写这些方法。第一个给出了拇指可以在其中移动拇指的矩形,第二个给出了拇指本身的位置。
I would like to know why none of you provide the simplest answer which consist in reading the manual. You can compute all these values accurately and also MAKING SURE THEY STAY THAT WAY, by simply using the methods:
which you can easily find in the developer documentation.
If thumb image changes and you want to change how it's positioned, you subclass and override these methods. The first one gives you the rectangle in which the thumb can move the second one the position of the thumb itself.
我通过首先以百分比映射 UISlider 的值区间,然后采用滑块大小的相同百分比减去拇指大小的百分比(我在该值上添加拇指大小的一半以获得其中心)来实现此目的。
I approached it by first mapping the UISlider's value interval in percents and then taking the same percent of the slider's size minus the percent of the thumb's size, a value to which I added half of the thumb's size to obtain its center.
斯威夫特3.0
喜欢的话请参考一下。
Swift 3.0
Please refer if you like.
稍微玩了一下 IB 和 1px 宽的拇指图像后,拇指的位置正是您所期望的位置:
AFter a little playing with IB and a 1px wide thumb image, the position of the thumb is exactly where you'd expect it:
这是一个 Swift 2.2 解决方案,我为其创建了一个扩展。我只尝试过使用默认图像。
Here is a Swift 2.2 solution, I created an extension for it. I have only tried this with the default image.
当 UISlider 水平时,上述解决方案很有用。在最近的一个项目中,我们需要使用带角度的UISlider。所以我需要获得 x 和 y 位置。使用下面的方法来计算x,y轴:
另外,我可以基于UISlider创建一个Ranger Slider吗?谢谢。
Above solution is useful when UISlider is horizontal. In a recent project,we need to use UISlider with angle. So I need to get both x and y position. Using below to calculate the x,y axis:
Besides, can I Create a Ranger Slider based on UISlider? Thanks.
这适用于将 UISlider 放置在屏幕上的任何位置。大多数其他解决方案仅在 UISlider 与屏幕左边缘对齐时才有效。请注意,我对拇指矩形使用了frame而不是bounds来实现这一点。我展示了两种变体,基于对 trackRect 使用
frame
或bounds
This will work for the UISlider being placed anywhere on the screen. Most of the other solutions will only work when the UISlider is aligned with the left edge of the screen. Note, I used
frame
rather thanbounds
for the thumbRect, to achieve that. And I show two variations, based on usingframe
orbounds
for the trackRect第 1 步:获取检测位置的视图(使用与 # Ovi Bortas 相同的扩展名顶部注释)
@IBOutlet weak var sliderView: UIView!
步骤2:设置标签框以添加子视图
func setLabelThumb(滑块:UISlider,值:Float){
滑块.值 = 值
}
step 1 :get View for detect position (use same extension top commet of# Ovi Bortas)
@IBOutlet weak var sliderView: UIView!
step 2 : set label frame for add sub view
func setLabelThumb(slider:UISlider,value:Float){
slider.value = value
}