How to get the center of the thumb image of UISlider

后端 未结 12 1849
挽巷
挽巷 2020-11-29 23:18

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, <

12条回答
  •  盖世英雄少女心
    2020-11-29 23:48

    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];
    

提交回复
热议问题