How can I create NSSlider animation when changing float value of it. I was trying:
[[mySlider animator] setFloatValue:-5];
but that didn't work.. just change the value without animation. So maybe someone knows how to do this?
Thanks in advance.
Ok - so this isn't as quick and pretty as I hoped but it works. You can't actually use animators and Core Animation on the slider knob - because Core Animation works only on layers and there's no access to the knob values in the slider layer.
So we have to resort instead to manually animating slider value. Since we're doing this on a Mac - you can use NSAnimation (which isn't available on iOS).
What NSAnimation does is simple - it provide an timing/interpolation mechanism to allow YOU to animate (as opposed to Core Animation which also connects to the views and handles the changes to them).
To use NSAnimation - you most commonly would subclass it and override
setCurrentProgress:
and put your logic in there.Here's how I implemented this - I created a new NSAnimation subclass called
NSAnimationForSlider
NSAnimationForSlider.h :
NSAnimationForSlider.m :
To use it - you simply create a new NSAnimationForSlider, give it the slider you are working on as a delegate and before each animation you set it's animateToValue and then just start the animation.
For example:
Your method works, but there's something much simpler.
You can use the animator proxy, you just need to tell it how to animate it. To do this, you need to implement the
defaultAnimationForKey:
method from theNSAnimatablePropertyContainer
protocol.Here's a simple subclass of
NSSlider
that does this:Now you can simply use the animator proxy:
Make sure to link the
QuartzCore
framework.