UIslider 缩略图不是从头开始的

UIslider thumb image doesn't start from the beginning

我正在尝试实现一个控件来显示视频的进度,我正在使用带有自定义缩略图的 UISlider,但缩略图不是从头开始,也不会一直到最后,因为出色地。

playerProgress = UISlider(frame: CGRectMake((btnImage.size.width + 2 * VideoViewControllerUX.ControlPadding), 0, (screenRect.size.width - (btnImage.size.width + 3 * VideoViewControllerUX.ControlPadding)), btnImage.size.height))
playerProgress.setThumbImage(UIImage(named: "slider"), forState: UIControlState.Normal)
playerProgress.maximumValue = 100
playerProgress.minimumValue = 0
playerProgress.addTarget(self, action: "playerProgressChanged:", forControlEvents: UIControlEvents.ValueChanged)

我不确定发生了什么。

您所看到的是正常的。滑块在两端留下一些额外的 space,以便当滑块的 edge 位于滑块框架的末端时,滑块处于最小值或最大值。

看看这些滑块。它们具有相同的水平位置和宽度:

第一个滑块的拇指已尽可能向左移动。它不会再向左, 轨道框架之外;当它的 edge 碰到轨道的框架时它会停止。那是零。

如果您不喜欢 where thumb image 是相对于整体轨道绘制的,您需要子类 UISlider 并实现 thumbRectForBounds:trackRect:value:.

我通过子类化 UISlider 创建了类似的滑块。

//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);
}