4

I'm creating todays extension within iOS. What I'm trying to do is to animate value change on UISlider added in todays extension. Animation should proceed from start value to end value.

I tried to do it in two ways: First UIView animation

func updateSliderView(beginState:Float, duration: NSTimeInterval) {
    sliderView.hidden = false
    sliderView.setValue(beginState, animated: false)
    UIView.animateWithDuration(duration) { () -> Void in
        self.sliderView.setValue(1, animated: true)
    }
}

Second with Basic Animation.

func updateSliderView(beginState:Float, duration: NSTimeInterval) {
    sliderView.hidden = false
    guard let _ = sliderView.layer.animationForKey("slider.animation.value") else {
        let animation = CABasicAnimation(keyPath: "value")
        animation.fromValue = beginState
        animation.toValue = 1
        animation.removedOnCompletion = true
        sliderView.layer.addAnimation(animation, forKey: "slider.animation.value")
        return
    }
}

I'm doing it with new XCode 7.0.1 with iOS target 8.1 In both result ends without animation. UISlider have User interaction disabled. I'm using UISluder because I have custom graphics for slider path min & max track and for the slider thumb.

I did third test

sliderView.setValue(beginState, animated: false)
CATransaction.begin()
CATransaction.setAnimationDuration(duration)
sliderView.setValue(1, animated: true)
CATransaction.commit()

it wont work :/

Summary: Only UIView.animation works. It's animating but it's breaking my customs graphic for UISlider and it's not setting start value for slider.

How can I animate UISlider value change in Todays extension??

Shial
  • 1,386
  • 19
  • 31

1 Answers1

3

Ok I solved it out. I answer my own question because maybe it will help to somebody and it's the solution I was asking for.

Only UIView.animation are animating UISlider inside Todays Extension. But setting starting value was not working before animation and the animation was breaking custom graphics on slider. I figured it out when you set starting value in UIView.animation block everthing will work as I wanted.

UIView.animateWithDuration(0.2, delay: 0, options: .CurveLinear, animations: { () -> Void in
        self.sliderView.setValue(beginState, animated: true)
        }) { (completed) -> Void in
            UIView.animateWithDuration(duration, delay: 0.1, options: .CurveLinear, animations: { () -> Void in
                self.sliderView.setValue(1, animated: true)
                }, completion: nil)
    }

I combined two blocks of UIView animation. The result was as I expected before. starting value was set. UISlider value was animated from starting value to the end. All custom graphics like min, max and thumb were rendered correctly.

Shial
  • 1,386
  • 19
  • 31