Sunday, December 3, 2023
HomeiOS Developmentios - Add ticker to snapping UISlider

ios – Add ticker to snapping UISlider

I’m having some points making a customized UISlider that snaps at sure values, and still have tick marks at these values. I’ve the snapping half working, however the tick marks is a matter. The Slider snaps in 12 positions, each 5 from 15-70. I’ve tried quite a lot of approaches, together with including 12 subviews to a stack view and inserting that on the slider, and in addition calculating the step width between every. Each approaches positioned the ticks not the place the thumb snaps to. Does anybody understand how to do that appropriately?

This is the second strategy:

        let stepCount = 12
        print("THE WIDTH: (self.bounds.width)")
        guard let imageWidth = self.currentThumbImage?.dimension.width else {return}

        guard let imageWidth = self.currentThumbImage?.dimension.width else {return}
        let stepWidth = bounds.width / CGFloat(stepCount)
        for i in 0..<stepCount {
            let view = UIView(body: CGRect(x: stepWidth / 2 + stepWidth * CGFloat(i) - imageWidth / 2, y: 0, width: 1, top: 29))
            view.backgroundColor = .lightGray
            self.insertSubview(view, at: 0)

Right here is the snapping code:

@objc func valueChanged(_ sender: UISlider){
        let step: Float = 5
        let roundedValue = spherical(sender.worth / step) * step
        self.worth = roundedValue
        delegate?.sliderChanged(Int(roundedValue), sender)



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments