Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to animate slider to value? #9

Open
9SL9 opened this issue Feb 25, 2017 · 8 comments
Open

How to animate slider to value? #9

9SL9 opened this issue Feb 25, 2017 · 8 comments

Comments

@9SL9
Copy link

9SL9 commented Feb 25, 2017

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

@wasappi
Copy link

wasappi commented Feb 28, 2017

Indeed, it be would be the perfect addition

@SanaElshazly
Copy link

+1

@OpenMarshall
Copy link

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.

@i-am-chris
Copy link

add a listener

@munibrahman
Copy link

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

@JMCPH
Copy link

JMCPH commented Aug 8, 2019

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

@asar1
Copy link

asar1 commented Apr 9, 2020

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

@oaleeapp
Copy link

oaleeapp commented Sep 14, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants