How to get the center of the thumb image of UISlider

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

    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:

    - (CGPoint)xyPositionFromSliderValue:(UISlider *)aSlider WithAngle:(double)aangle{
       //aangle means the dextrorotation angle compare to horizontal.
       float xOrigin = 0.0;
       float yOrigin = 0.0;
       float xValueToaXis=0.0;
       float yValueToaXis=0.0;
       float sliderRange = slider_width-aSlider.currentThumbImage.size.width;
       xOrigin = aSlider.frame.origin.x+slider_width*fabs(cos(aangle/180.0*M_PI));
       yOrigin = aSlider.frame.origin.y;
    
       xValueToaXis = xOrigin + ((((((aSlider.value-aSlider.minimumValue)/(aSlider.maximumValue-aSlider.minimumValue)) * sliderRange))+(aSlider.currentThumbImage.size.width / 2.0))*cos(aangle/180.0*M_PI)) ;
       yValueToaXis =  yOrigin + ((((((aSlider.value-aSlider.minimumValue)/(aSlider.maximumValue-aSlider.minimumValue)) * sliderRange))+(aSlider.currentThumbImage.size.width / 2.0))*sin(aangle/180.0*M_PI));
    
       CGPoint xyPoint=CGPointMake(xValueToaXis, yValueToaXis);
       return xyPoint;
    }
    

    Besides, can I Create a Ranger Slider based on UISlider? Thanks.

    0 讨论(0)
  • 2020-11-29 23:37

    Swift 3

    extension UISlider {
        var thumbCenterX: CGFloat {
            let trackRect = self.trackRect(forBounds: frame)
            let thumbRect = self.thumbRect(forBounds: bounds, trackRect: trackRect, value: value)
            return thumbRect.midX
        }
    }
    
    0 讨论(0)
  • 2020-11-29 23:37

    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:

    - (CGRect)trackRectForBounds:(CGRect)bounds
    - (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value
    

    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.

    0 讨论(0)
  • 2020-11-29 23:37

    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 than bounds for the thumbRect, to achieve that. And I show two variations, based on using frame or bounds for the trackRect

    extension UISlider {
    
        //this version will return the x coordinate in relation to the UISlider frame
        var thumbCenterX: CGFloat {
            return thumbRect(forBounds: frame, trackRect: trackRect(forBounds: bounds), value: value).midX
        }
    
        //this version will return the x coordinate in relation to the UISlider's containing view
        var thumbCenterX: CGFloat {
            return thumbRect(forBounds: frame, trackRect: trackRect(forBounds: frame), value: value).midX
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 23:43

    Swift 3.0
    Please refer if you like.

    import UIKit
    
    extension UISlider {
    
        var trackBounds: CGRect {
            return trackRect(forBounds: bounds)
        }
    
        var trackFrame: CGRect {
            guard let superView = superview else { return CGRect.zero }
            return self.convert(trackBounds, to: superView)
        }
    
        var thumbBounds: CGRect {
            return thumbRect(forBounds: frame, trackRect: trackBounds, value: value)
        }
    
        var thumbFrame: CGRect {
            return thumbRect(forBounds: bounds, trackRect: trackFrame, value: value)
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题