答案 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