UIslider拇指图像不从头开始

时间:2015-06-21 18:19:48

标签: ios swift uislider

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

enter image description here

我不确定最新情况。

拇指图像:enter image description here

2 个答案:

答案 0 :(得分:5)

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

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

enter image description here

第一个滑块的拇指尽可能向左移动。它不会向左走,轨道的框架之外;它的边缘击中轨道的框架时停止。那是零。

如果你不喜欢 正在绘制与整个音轨相关的拇指图像,那么你需要将UISlider子类化并实现thumbRectForBounds:trackRect:value:

答案 1 :(得分:-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);
}