Re: Animated NSSliders: the code.
Re: Animated NSSliders: the code.
- Subject: Re: Animated NSSliders: the code.
- From: "John C. Randolph" <email@hidden>
- Date: Tue, 6 Nov 2001 05:45:53 -0800
On Tuesday, November 6, 2001, at 03:32 AM, John C. Randolph wrote:
After seeing the spiffy animation in the equalizer window of iTunes 2,
I just knocked out a category of NSSlider to animate motion to a given
value. If anyone wants it, I'd be glad to send you the code. It's
only 95 lines.
Okay, there have been a surprisingly large number of people asking for
it, and it is pretty small, so I'll just post it here.
//
// NSSlider_Animation.h
// AnimatedSlider
//
// Created by jcr on Tue Nov 06 2001.
// Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//
#import <AppKit/AppKit.h>
@interface NSSlider (MiscSliderAnimation)
- (IBAction) animateToFloatValueFrom: sender;
- (void) animateToFloatValue:(float) newValue;
@end
extern float MiscSliderAnimationInterval;
extern float MiscSliderAnimationSteps;
//
// NSSlider_Animation.m
// AnimatedSlider
//
// Created by jcr on Tue Nov 06 2001.
// Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//
#import "NSSlider_Animation.h"
float MiscSliderAnimationInterval;
float MiscSliderAnimationSteps;
@interface NSSlider (MiscSliderAnimationPrivate)
- (void) _animationStep: sender; // This is in a private category,
because this should only ever be sent by the NSTimer created in
-animateToFloatValue:.
@end
@implementation NSSlider (MiscSliderAnimationPrivate)
- (void) _animationStep:sender
{
id
parms = [sender userInfo];
float
endValue, currentValue, step;
step = [[parms valueForKey:@"step"] floatValue];
endValue = [[parms valueForKey:@"end"] floatValue];
currentValue = [self floatValue] + step;
currentValue = (step > 0 ? MIN(currentValue, endValue) :
MAX(currentValue, endValue));
[self setFloatValue: currentValue];
if ([self isContinuous]) // When the slider moves, the target should
get a message..
[self sendAction:[self action] to:[self target]];
if (currentValue == endValue)
{
[self sendAction:[self action] to:[self target]]; // Make it act
just like the user did the moving.
[sender invalidate];
[sender release];
}
}
@end
@implementation NSSlider (MiscSliderAnimation)
+ (void) initialize
{ // tweak these values to your taste.
MiscSliderAnimationInterval = 0.004; // smaller number = faster
"frame rate"
MiscSliderAnimationSteps = 40; // bigger number = smaller
steps.
}
- (IBAction) animateToFloatValueFrom:sender
{
[self animateToFloatValue:[sender floatValue]];
}
- (void) animateToFloatValue:(float) newValue
{
[NSTimer scheduledTimerWithTimeInterval:MiscSliderAnimationInterval
target:self
selector:@selector(_animationStep:)
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: ((newValue - [self floatValue]) /
MiscSliderAnimationSteps)], @"step",
[NSNumber numberWithFloat:newValue], @"end",
[NSDate date], @"startDate", nil]
repeats:YES];
}
@end
[Objc retain];