Good - it’s not as fast and beautiful as I hoped, but it works. In fact, you cannot use animators and Core Animation on the slider handle, because Core Animation works only on layers and there is no access to the values of the grips in the slider layer.
Therefore, we must resort to manually animating the values of the slider. Since we do this on a Mac, you can use NSAnimation (which is not available on iOS).
NSAnimation - /, ( Core Animation, ).
NSAnimation - setCurrentProgress:
.
- NSAnimation NSAnimationForSlider
NSAnimationForSlider.h:
@interface NSAnimationForSlider : NSAnimation
{
NSSlider *delegateSlider;
float animateToValue;
double max;
double min;
float initValue;
}
@property (nonatomic, retain) NSSlider *delegateSlider;
@property (nonatomic, assign) float animateToValue;
@end
NSAnimationForSlider.m:
#import "NSAnimationForSlider.h"
@implementation NSAnimationForSlider
@synthesize delegateSlider;
@synthesize animateToValue;
-(void)dealloc
{
[delegateSlider release], delegateSlider = nil;
}
-(void)startAnimation
{
initValue = [delegateSlider floatValue];
if (animateToValue >= initValue) {
min = initValue;
max = animateToValue;
} else {
min = animateToValue;
max = initValue;
}
[super startAnimation];
}
- (void)setCurrentProgress:(NSAnimationProgress)progress
{
[super setCurrentProgress:progress];
double newValue;
if (animateToValue >= initValue) {
newValue = min + (max - min) * progress;
} else {
newValue = max - (max - min) * progress;
}
[delegateSlider setDoubleValue:newValue];
}
@end
- NSAnimationForSlider, , , animateToValue, .
:
slider = [[NSSlider alloc] initWithFrame:NSMakeRect(50, 150, 400, 25)];
[slider setMaxValue:200];
[slider setMinValue:50];
[slider setDoubleValue:50];
[[window contentView] addSubview:slider];
NSAnimationForSlider *sliderAnimation = [[NSAnimationForSlider alloc] initWithDuration:2.0 animationCurve:NSAnimationEaseIn];
[sliderAnimation setAnimationBlockingMode:NSAnimationNonblocking];
[sliderAnimation setDelegateSlider:slider];
[sliderAnimation setAnimateToValue:150];
[sliderAnimation startAnimation];