HGCircularSlider
HGCircularSlider copied to clipboard
How to animate slider to value?
I was just wondering if there is a way to animate (without user interaction) the slider to a specific value?
Indeed, it be would be the perfect addition
+1
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.
add a listener
@i-am-chris Do you mind sharing some code? Thanks
Did anyone manage to get the animation of endpoint to work? Then we could use this as progressive which could be interacted with.
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
}
}`
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))
}
}