Skip to content

Instantly share code, notes, and snippets.

@pixelrevision
Created April 12, 2011 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelrevision/914991 to your computer and use it in GitHub Desktop.
Save pixelrevision/914991 to your computer and use it in GitHub Desktop.
SXSlider.h
#import <Foundation/Foundation.h>
#import "Sparrow.h"
#define SX_SLIDER_EVENT_UPDATED @"sliderUpdated"
@interface SXSlider : SPSprite {
float value;
float min;
float max;
float percentage;
SPDisplayObject *handle;
SPDisplayObject *track;
}
@property (nonatomic, readonly) float value;
@property (nonatomic, assign) float min;
@property (nonatomic, assign) float max;
@property (nonatomic, assign) float percentage;
@property (nonatomic, retain) SPDisplayObject *handle;
@property (nonatomic, retain) SPDisplayObject *track;
- (void)update;
- (id)initWithHandlePath:(NSString*)handlePath andTrackPath:(NSString*)trackPath;
+ (SXSlider*)sliderWithHandlePath:(NSString*)handlePath andTrackPath:(NSString*)trackPath;
@end
#import "SXSlider.h"
@implementation SXSlider
@synthesize handle;
@synthesize track;
@synthesize value;
@synthesize min;
@synthesize max;
@synthesize percentage;
- (id)init{
self = [super init];
min = 0;
max = 1;
percentage = 1;
self.handle = [SPQuad quadWithWidth:10 height:10 color:0xc0c0c0];
self.track = [SPQuad quadWithWidth:100 height:10 color:0x3d3d3d];
return self;
}
- (id)initWithHandlePath:(NSString*)handlePath andTrackPath:(NSString*)trackPath{
self = [super init];
min = 0;
max = 1;
percentage = 1;
self.handle = [SPImage imageWithContentsOfFile:handlePath];
self.track = [SPImage imageWithContentsOfFile:trackPath];
return self;
}
+ (SXSlider*)sliderWithHandlePath:(NSString*)handlePath andTrackPath:(NSString*)trackPath{
return [[[SXSlider alloc] initWithHandlePath:handlePath andTrackPath:trackPath] autorelease];
}
- (void)setHandle:(SPDisplayObject *)val{
if(handle){
if([self containsChild:handle]){
[self removeChild:handle];
[handle removeEventListener:@selector(touched:) atObject:self forType:SP_EVENT_TYPE_TOUCH];
}
[handle release];
handle = nil;
}
if(val != nil){
handle = [val retain];
[self addChild:handle];
[handle addEventListener:@selector(touched:) atObject:self forType:SP_EVENT_TYPE_TOUCH];
}
[self update];
}
- (void)setTrack:(SPDisplayObject *)val{
if(track){
if([self containsChild:track]){
[self removeChild:track];
}
[track release];
track = nil;
}
if(val != nil){
track = [val retain];
[self addChild:track atIndex:0];
}
[self update];
}
- (float)value{
return (percentage*(max-min)) + min;
}
- (void)touched:(SPTouchEvent*)event{
SPTouch *touchMoved = [[event touchesWithTarget:self andPhase:SPTouchPhaseMoved] anyObject];
if(touchMoved){
SPPoint *touchPosition = [touchMoved locationInSpace:self];
if(touchPosition.x < 0) touchPosition.x = 0;
if(touchPosition.x > (track.width - handle.width)) touchPosition.x = (track.width - handle.width);
handle.x = touchPosition.x;
percentage = handle.x/(track.width - handle.width);
[self dispatchEvent:[SPEvent eventWithType:SX_SLIDER_EVENT_UPDATED]];
}
}
- (void)setPercentage:(float)val{
percentage = val;
[self update];
}
- (void)update{
if(self.track != nil && self.handle != nil){
handle.x = percentage * (track.width - handle.width);
}
}
- (void)dealloc{
self.handle = nil;
self.track = nil;
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment