gpt4 book ai didi

ios - 自定义 UISlider 不会结束

转载 作者:可可西里 更新时间:2023-11-01 02:00:15 26 4
gpt4 key购买 nike

我创建了一个 UISlider,它具有刻度线并在值更改时捕捉到每个刻度线。问题是 slider 不会自然地走到尽头,左右刻度在圆圈外可见。我已将 UISlider 类扩展到末端,但动画与将 slider 移动到下一个刻度线发生冲突,并且圆圈最终离开了屏幕。我怎样才能保持捕捉动画并让 slider 中的圆圈一直走到尽头?这是我的代码

class CustomSlider: UISlider {

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)
}
}

@IBOutlet weak var slider: CustomSlider!

let tickArray : [Float] = [Float(18),Float(20),Float(22),Float(24),Float(26),Float(28),Float(30)]

override func viewDidLoad() {
super.viewDidLoad()

self.slider.value = tickArray[0]
self.slider.minimumValue = tickArray[0]
self.slider.maximumValue = tickArray[6]
self.slider.isContinuous = false



var tick : UIView
for i in 0..<tickArray.count-1 {
tick = UIView(frame: CGRect(x: (self.slider.frame.size.width/6) * CGFloat(i), y: (slider.frame.size.height - 13) / 2, width: 2, height: 13))
tick.backgroundColor = "8E8E93".hexColor
slider.insertSubview(tick, belowSubview: slider)
}

tick = UIView(frame: CGRect(x: self.slider.frame.size.width-2, y: (slider.frame.size.height - 13) / 2, width: 2, height: 13))
tick.backgroundColor = "8E8E93".hexColor
slider.insertSubview(tick, belowSubview: slider)

}

@IBAction func sliderValueChanged(_ sender: UISlider) {


if sender.value > tickArray[5] {
slider.value = tickArray[6]
} else if sender.value > tickArray[4] {
slider.value = tickArray[5]
} else if sender.value > tickArray[3] {
slider.value = tickArray[4]
} else if sender.value > tickArray[2] {
slider.value = tickArray[3]
} else if sender.value > tickArray[1] {
slider.value = tickArray[2]
} else if sender.value >
tickArray[0] {
slider.value = tickArray[1]
} else {
slider.value = tickArray[0]
}
}

enter image description here

最佳答案

我通过在 slider 移动到边缘时隐藏左右刻度来解决我的问题。这消除了对 slider 扩展的需要。

关于ios - 自定义 UISlider 不会结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46980022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com