问题描述
我正在创建自定义 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.
查看 UISlider 文档,我没有看到任何跟踪此属性的属性。
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)?
推荐答案
这将返回UISlider在视图坐标中的正确X轴位置:
This will return the correct X position of center of thumb image of UISlider in view coordinates:
- (float)xPositionFromSliderValue:(UISlider *)aSlider {
float sliderRange = aSlider.frame.size.width - aSlider.currentThumbImage.size.width;
float sliderOrigin = aSlider.frame.origin.x + (aSlider.currentThumbImage.size.width / 2.0);
float sliderValueToPixels = (((aSlider.value - aSlider.minimumValue)/(aSlider.maximumValue - aSlider.minimumValue)) * sliderRange) + sliderOrigin;
return sliderValueToPixels;
}
将它放在视图控制器中并使用它:滑块)
Put it in your view controller and use it like this: (assumes property named slider)
float x = [self xPositionFromSliderValue:self.slider];
这篇关于如何获取UISlider的拇指图像的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!