【问题标题】:UIslider thumb image doesn't start from the beginningUIslider 拇指图像不会从头开始
【发布时间】:2019-07-24 21:55:34
【问题描述】:

我正在尝试实现一个控件来显示视频的进度,并且我正在使用带有自定义拇指图像的 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)

我不确定发生了什么。

  • 拇指图:

【问题讨论】:

  • 拇指图像应该是什么样子?这个控件对我来说看起来很正常。
  • @CraigOtis 我已经添加了拇指图像(它是白色的,只需右键单击文本旁边并下载)。

标签: ios swift uislider


【解决方案1】:

你看到的是正常的。滑块在两端留有一些额外空间,因此当拇指的边缘位于滑块框架的末端时,拇指处于最小值或最大值。

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

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

如果您不喜欢 where 相对于整个轨道绘制拇指 图像,则需要继承 UISlider 并实现 thumbRectForBounds:trackRect:value:

【讨论】:

  • 是的,在发布问题之前我已经按照您的建议进行了操作。当我覆盖 thumbRectForBounds:trackRect:value: 滑块停止工作(它不滑动)。
  • 覆盖 func thumbRectForBounds(bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect { let thumbImage = UIImage(named: "slider")! let thumbImageSize = thumbImage.size return CGRect(x: -10, y: 0, width: thumbImageSize.width, height: thumbImageSize.height) } 我已经添加了上面的代码然后滑块停止工作(不移动)跨度>
  • 也许我应该更清楚。您需要正确实现thumbRectForBounds:trackRect:value:。显然你的实现是荒谬的:如果你给出相同的返回矩形,不管传入的value是什么,滑块都不会移动。
  • @matt 即使您将拇指图像设置为与slider.setThumbImage(slider.thumbImageForState(.Normal), forState: .Normal) 相同的情况,这实际上也会发生,唯一的要求是它必须是一些自定义图像(例如不同的拇指色调)。如果你想知道为什么有人会这样做,是因为:stackoverflow.com/questions/20903317/…
  • 我用同样的方法解决了这个问题:stackoverflow.com/questions/40502400/…
【解决方案2】:

我通过子类化 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多