Thumb image does not move to the edge of UISlider

后端 未结 9 1154
有刺的猬
有刺的猬 2021-02-13 02:14

Thumb image does not move to the edge even when it\'s value is max or min.

Does anyone know how to make it move all the way to the edge of the slider?

9条回答
  •  清歌不尽
    2021-02-13 03:02

    I have created the similar slider by subclassing UISlider. In storyboard you can set a height constraint to increase height at runtime if slider thumb not gets touch events.

    static float const SLIDER_OFFSET = 10.0;
    
    //Get thumb rect for larger track rect than actual to move slider to edges
        -(CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value {
            UIImage *image = self.currentThumbImage;
    
        rect.origin.x -= SLIDER_OFFSET;
        rect.size.width += (SLIDER_OFFSET*2);
        CGRect thumbRect = [super thumbRectForBounds:bounds trackRect:rect value:value];
        return CGRectMake(thumbRect.origin.x, rect.origin.y+2, image.size.width, image.size.height);
    }
    
    //Make track rect smaller than bounds
    -(CGRect)trackRectForBounds:(CGRect)bounds  {
        bounds.origin.x += SLIDER_OFFSET;
        bounds.size.width -= (SLIDER_OFFSET*2);
        CGRect trackRect = [super trackRectForBounds:bounds];
    
        return CGRectMake(trackRect.origin.x, trackRect.origin.y, trackRect.size.width, trackRect.size.height);
    }
    

提交回复
热议问题