Why is my UISlider not animating?

According to the Apple documentation , you can programmatically set a UISlider value with smooth animation. I am trying to do this with a custom view controller, the user interface is determined from the storyboard.

Context

In my example, I am trying to update the value of a slider with a custom view controller, the user interface is determined from the storyboard. The example shows only one slider.

When the user releases the slider, the reset value is 0 .

The code

 import UIKit class ViewController: UIViewController { @IBOutlet var mySlider: UISlider! @IBAction func resetSlider() { mySlider.setValue(0, animated:true) NSLog("Reset!") } override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 

resetSlider is associated with the Touch Up Inside event.

Problem

When resetSlider is called, the value changes on the interface, but it is not animated (the value simply "jumps" to 0). My goal is for the value to gracefully shift back to zero.

Note: "Reset!" displayed only once (per click), which indicates that resetSlider not called multiple times.

Why not UISlider animation?

Video

Since IB is so visual, here is a video about the situation, password code

+8
ios animation swift
source share
2 answers

The setValue animated parameter does not actually perform the animation, but turns on the animation.

To start the animation, you need to use UIView.animateWithDuration and pass the setValue command as the animation:

 UIView.animateWithDuration(0.2, animations: { self.mySlider.setValue(0, animated:true) }) 
+28
source share

@Slifty's solution is correct. But if you want the UISlider to be as flat as defauft, you can reduce the time animation to less than 0.2. It can work, for example, 0.1 / 2 or 0.1 / 3.

0
source share

All Articles