Skip to content

Instantly share code, notes, and snippets.

@Noiled
Forked from liscio/CapoPlaybackButton.h
Last active May 15, 2017 09:35
Show Gist options
  • Save Noiled/26ed75f40dbef827c046526db1723db1 to your computer and use it in GitHub Desktop.
Save Noiled/26ed75f40dbef827c046526db1723db1 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 = (1 << 16),
kCapoPlaybackButtonStatePlay = (1 << 17),
};
@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