我试图在UISlider轨道上获得特定值的X,Y位置,以便在甚至用拇指移动滑块之前就可以在该位置放置文本字段。我希望能够在轨道上放置多个文本字段,以显示该位置的值。
有人可以帮助我实现这一目标吗?
谢谢
答案 0 :(得分:3)
let values: [Float] = [15, 30, 50, 70, 85]
for value in values {
guard (slider.minimumValue...slider.maximumValue).contains(value) else { continue }
let label = UILabel()
label.text = "\(value)"
view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
let thumbRect = slider.thumbRect(forBounds: slider.bounds,
trackRect: slider.trackRect(forBounds: slider.bounds),
value: value)
let convertedThumbRect = slider.convert(thumbRect, to: view)
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: view.leadingAnchor, constant: convertedThumbRect.midX),
slider.topAnchor.constraintEqualToSystemSpacingBelow(label.bottomAnchor, multiplier: 1)
])
}
答案 1 :(得分:1)
首先,您将找到UISlide拇指图像的框架。
对于UISlide缩略图图像的查找框架,请使用以下方法:-
@IBAction func sliderAction(_ sender: UISlider) {
let _thumbRect: CGRect = sender.thumbRect(forBounds: sender.bounds, trackRect: sender.trackRect(forBounds: sender.bounds), value: sender.value)
let thumbRect: CGRect = view.convert(_thumbRect, from: sender)
print("thumbRect \(thumbRect)")
}