UISlider缩略图位置偏移

时间:2017-07-20 17:49:25

标签: ios uislider

我正在尝试使用自定义图像实现标准UISlider。

目前它看起来像是

enter image description here

但我需要它看起来像这样

enter image description here

如何将缩略图偏移,以便只有尖端几乎不接触线?

2 个答案:

答案 0 :(得分:2)

如果要更改缩略图的偏移量,则必须在自定义thumbRectForBounds:trackRect:value:子类中覆盖UISlider

class CustomSlider: UISlider {
    override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
        let originalRect = super.thumbRect(forBounds: bounds, trackRect: rect, value: value)
        let yOffset: CGFloat = 16 // any value you want
        let newY = originalRect.origin.y - yOffset

        return CGRect(x: originalRect.origin.x, y: newY, width: originalRect.width, height: originalRect.height)
    }
}

答案 1 :(得分:0)

slideBar的值更改后,可以通过以下代码获取拇指位置。

    let slideBounds = self.slideBar.bounds
    let trackRt = self.slideBar.trackRect(forBounds: slideBounds)

    // thumbRt is in slideBar coordination.
    let thumbRt = self.slideBar.thumbRect(forBounds: slideBounds, trackRect: trackRt, value: slideBar.value)

    // Get center point of slideBar with coordination of `self`.
    let centerPt = self.convert(CGPoint(x: thumbRt.midX, y: thumbRt.midY), from: self.slideBar)

    // Let a UILabel moving with thumb image of slideBar.
    var origPt = self.lblValue.center
    origPt.x = centerPt.x
    self.lblValue.center = origPt