title: Loop UISlider from left to right and vice versa in Swift
body: <p>I want to move a <code>UISlider</code> from left to right and as soon as it hits <code>maximumValue</code> it shall move to the left until it hits <code>minimumValue</code> and then move again to <code>maximumValue</code>. </p>
  
  <p>I'm using this code including a timer for slow movement. </p>
  
  <p>But the problem is as soon as it hits <code>maximumValue</code> it moves one increment behind the first if cause gets triggered and it moves forwards again leading the slider to jump one step back and forth in a loop as soon as <code>maximumValue</code> was hit.</p>
  
  <p>I've already tried using the backwards boolean variable to tell the if cause it shall only move backwards but initial false value gets picked up very 'round'.</p>
  
  <p>I hope you got the concept.</p>
  
  <pre><code>@IBAction func setSliderValue(_ sender: UIButton){
          mytimer =  Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
      }
  
  
      func timerAction(){
          let Range =  slider.maximumValue - slider.minimumValue;
          let Increment = Range/100;
          var newval = slider.value + Increment;
          var backwards : Bool = false
  
          if(slider.value != slider.maximumValue &amp;&amp; backwards == false){
              slider.setValue(newval, animated: true)
              print("The value of the slider is now \(slider.value)")
              sliderValue = Int(slider.value)
              print("1. \(backwards)")
          }
          else if (slider.value == slider.maximumValue || slider.value != slider.maximumValue){
              newval = slider.value - 1
              print("newval : \(newval)")
              print("slider.value : \(slider.value)")
              slider.setValue(newval, animated: true)
              backwards = true
              print("2. \(backwards)")
          }
      }
  </code></pre>