HGCircularSlider icon indicating copy to clipboard operation
HGCircularSlider copied to clipboard

How to animate slider to value?

Open 9SL9 opened this issue 8 years ago • 8 comments

I was just wondering if there is a way to animate (without user interaction) the slider to a specific value?

9SL9 avatar Feb 25 '17 07:02 9SL9

Indeed, it be would be the perfect addition

wasappi avatar Feb 28 '17 13:02 wasappi

+1

SanaElshazly avatar Aug 21 '17 13:08 SanaElshazly

let circle = CircularSlider()
circle.minimumValue = 0
circle.maximumValue = 600
circle.endPointValue = 0
/* ...... */
func animateCircleAround() {
    if circle.endPointValue != circle.maximumValue {
        circle.endPointValue = circle.endPointValue + 1
        NSObject.cancelPreviousPerformRequests(withTarget: self)
        perform(#selector(animateCircleAround), with: nil, afterDelay: 3/circle.maximumValue)
    }else {
        circle.endPointValue = 0
    }
}

With this function, you can make a CircularSlider instance run 360°. But this is not a nice implementation, simply circle.endPointValue = circle.endPointValue + 1 cannot make a smooth animation. Hoping someone offer a better solution.

OpenMarshall avatar Sep 12 '17 09:09 OpenMarshall

add a listener

i-am-chris avatar Oct 30 '17 05:10 i-am-chris

@i-am-chris Do you mind sharing some code? Thanks

munibrahman avatar Feb 18 '18 10:02 munibrahman

Did anyone manage to get the animation of endpoint to work? Then we could use this as progressive which could be interacted with.

JMCPH avatar Aug 08 '19 11:08 JMCPH

Not a very good approach, but works for me

self.updateTimer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.animateSlider), userInfo: nil, repeats: true)

@objc private func animateSlider(){
    self.sliderView.endPointValue = CGFloat(currentCount)
    self.centerTextLbl?.text = "\(String(currentCount)) kg"
    currentCount += 1
    if currentCount > maxValueForAnim {
        self.updateTimer?.invalidate()
        self.updateTimer = nil
    }
}`

asar1 avatar Apr 09 '20 18:04 asar1

you can also use the func below, the same concept:

@objc private func animateProgress(_ progressBar: CircularSlider,from fromValue: CGFloat, to toValue: CGFloat){
        let numberOfSteps = 30
        let duration = 0.5
        let stepValue = (toValue - fromValue) / CGFloat(numberOfSteps)
        let delay = duration / Double(numberOfSteps)
        
        func setEndPointValue(_ progressBar: CircularSlider, to value: CGFloat, delay: Double = 0) {
            DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
                progressBar.endPointValue = value
            }
        }
        
        for step in 0...numberOfSteps {
            setEndPointValue(progressBar, to: fromValue + stepValue * CGFloat(step), delay: delay * Double(step))
        }
    }

oaleeapp avatar Sep 14 '20 03:09 oaleeapp