NSSlider Animation

How to create an NSSlider animation when changing a float value. I have tried:

[[mySlider animator] setFloatValue:-5];

but it didn’t work. Just change the value without animation. So maybe someone knows how to do this?

Thanks in advance.

+5
source share
2 answers

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
{
    //Setup initial values for every animation
    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];
+5

, - .

- , , . defaultAnimationForKey: NSAnimatablePropertyContainer.


NSSlider, :

#import "NSAnimatableSlider.h"
#import <QuartzCore/QuartzCore.h>

@implementation NSAnimatableSlider

+ (id)defaultAnimationForKey:(NSString *)key
{
    if ([key isEqualToString:@"doubleValue"]) {
        return [CABasicAnimation animation];
    } else {
        return [super defaultAnimationForKey:key];
    }
}

@end

- :

[self.slider.animator setDoubleValue:100.0];

QuartzCore.

+2

All Articles