Skip to content

Instantly share code, notes, and snippets.

@liscio
Created September 22, 2010 21:22
Show Gist options
  • Save liscio/592599 to your computer and use it in GitHub Desktop.
Save liscio/592599 to your computer and use it in GitHub Desktop.
//
// CapoPlaybackButton.h
// Capo
//
// Created by Christopher Liscio on 9/22/10.
// Copyright (c) 2010 SuperMegaUltraGroovy. All rights reserved.
//
#import <UIKit/UIKit.h>
enum {
kCapoPlaybackButtonStatePause = 0x00010000,
kCapoPlaybackButtonStatePlay = 0x00020000,
};
@interface CapoPlaybackButton : UIButton {
}
@property (nonatomic,assign) NSInteger playbackState;
@end
//
// CapoPlaybackButton.m
// Capo
//
// Created by Christopher Liscio on 9/22/10.
// Copyright (c) 2010 SuperMegaUltraGroovy. All rights reserved.
//
#import "CapoPlaybackButton.h"
static void *kPlaybackStateObservingContext = &kPlaybackStateObservingContext;
@implementation CapoPlaybackButton
@synthesize playbackState;
- (void)awakeFromNib {
[self setImage:[UIImage imageNamed:@"PauseN"] forState:( kCapoPlaybackButtonStatePause )];
[self setImage:[UIImage imageNamed:@"PauseP"] forState:( kCapoPlaybackButtonStatePause | UIControlStateHighlighted )];
[self setImage:[UIImage imageNamed:@"PlayN"] forState:( kCapoPlaybackButtonStatePlay )];
[self setImage:[UIImage imageNamed:@"PlayP"] forState:( kCapoPlaybackButtonStatePlay | UIControlStateHighlighted )];
NSAssert( kCapoPlaybackButtonStatePause & UIControlStateApplication, @"Custom state not within UIControlStateApplication" );
NSAssert( kCapoPlaybackButtonStatePlay & UIControlStateApplication, @"Custom state not within UIControlStateApplication" );
[self addObserver:self forKeyPath:@"playbackState" options:NSKeyValueObservingOptionOld context:kPlaybackStateObservingContext];
}
- (UIControlState)state {
NSInteger returnState = [super state];
return ( returnState | self.playbackState );
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"playbackState"];
[super dealloc];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ( context == kPlaybackStateObservingContext ) {
NSInteger oldState = [[change objectForKey:NSKeyValueChangeOldKey] integerValue];
if ( oldState != self.playbackState ) {
[self layoutSubviews];
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment