自定义UISlider拇指未在轨道边缘上滑动

时间:2019-01-24 20:10:52

标签: ios swift uislider

我有一个自定义的uislider,效果很好,但是当拇指到达音轨的任一边缘时,它需要在正确的位置上轻轻触摸一下,拇指才能做出响应并再次滑动。对于用户而言,这会出现拇指感觉卡在边缘的情况。

我尝试通过将thumbRect插入项设置为负值来扩展CGRect,但是它所做的只是扩展拇指图像,并不一定固定拇指滑动的响应性。 / p>

func setupThumb() {       
    do {           
        self.setThumbImage(sliderThumbImage, for: .normal)
        self.setThumbImage(sliderThumbImage, for: .highlighted)
    } catch {
        //
    }
}


override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect
{
    let unadjustedThumbrect = super.thumbRect(forBounds: bounds, trackRect: rect, value: value)
    let thumbOffsetToApplyOnEachSide:CGFloat = unadjustedThumbrect.size.width / 2.0
    let minOffsetToAdd = -thumbOffsetToApplyOnEachSide
    let maxOffsetToAdd = thumbOffsetToApplyOnEachSide
    let offsetForValue = minOffsetToAdd + (maxOffsetToAdd - minOffsetToAdd) * CGFloat(value / (self.maximumValue - self.minimumValue))
    var origin = unadjustedThumbrect.origin
    origin.x += offsetForValue

    return CGRect(origin: origin, size: unadjustedThumbrect.size)
}

0 个答案:

没有答案