How to get the center of the thumb image of UISlider

后端 未结 12 1857
挽巷
挽巷 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:44

    I tried this after reading the above suggestion -

    yourLabel = [[UILabel alloc]initWithFrame:....];
    
    //Call this method on Slider value change event
    
    -(void)sliderValueChanged{
        CGRect trackRect = [self.slider trackRectForBounds:self.slider.bounds];
        CGRect thumbRect = [self.slider thumbRectForBounds:self.slider.bounds
                                   trackRect:trackRect
                                       value:self.slider.value];
    
        yourLabel.center = CGPointMake(thumbRect.origin.x + self.slider.frame.origin.x,  self.slider.frame.origin.y - 20);
    }
    

    For Swift version

    func sliderValueChanged() -> Void {
            let trackRect =  self.slider.trackRect(forBounds: self.slider.bounds)
            let thumbRect = self.slider.thumbRect(forBounds: self.slider.bounds, trackRect: trackRect, value: self.slider.value)
            yourLabel.center = CGPoint(x: thumbRect.origin.x + self.slider.frame.origin.x + 30, y: self.slider.frame.origin.y - 60)
        }
    

    I could get most accurate value by using this snippet.

提交回复
热议问题