Created
May 4, 2011 02:18
-
-
Save BlackFrog1/954637 to your computer and use it in GitHub Desktop.
Blinking Label Operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BlinkingColors.h | |
#import <Foundation/Foundation.h> | |
@interface BlinkingColors : NSObject { | |
@private | |
UIColor *back_; | |
UIColor *text_; | |
} | |
@property (nonatomic, readonly) UIColor *backgroundColor; | |
@property (nonatomic, readonly) UIColor *textColor; | |
- (id)init; | |
- (id)initWithBackgroundColor:(UIColor *)back textColor:(UIColor *)text; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BlinkingColors.m | |
#import "BlinkingColors.h" | |
@implementation BlinkingColors | |
@synthesize backgroundColor = back_; | |
@synthesize textColor = text_; | |
- (id)init { | |
[self release]; | |
@throw [NSException exceptionWithName:NSInternalInconsistencyException | |
reason:@"-init is not a valid initializer for the class BlinkingColors" | |
userInfo:nil]; | |
return nil; | |
} | |
- (id)initWithBackgroundColor:(UIColor *)back textColor:(UIColor *)text { | |
self = [super init]; | |
if (self) { | |
back_ = [back retain]; | |
text_ = [text retain]; | |
} | |
return self; | |
} | |
- (void)dealloc { | |
[back_ release]; | |
[text_ release]; | |
[super dealloc]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BlinkingLabelOperation.h | |
#import <Foundation/Foundation.h> | |
@class BlinkingColors; | |
@interface BlinkingLabelOperation : NSOperation { | |
@private | |
BOOL mode_; | |
UILabel *label_; | |
NSTimeInterval interval_; | |
BlinkingColors *blinkColors_; | |
BlinkingColors *normalColors_; | |
} | |
- (id)init; | |
- (id)initWithLabel:(UILabel *)label freq:(NSTimeInterval)interval blinkColors:(BlinkingColors *)blink; | |
- (id)initWithLabel:(UILabel *)label freq:(NSTimeInterval)interval | |
normalColors:(BlinkingColors *)normal blinkColors:(BlinkingColors *)blink; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BlinkingLabelOperation.m | |
#import "BlinkingLabelOperation.h" | |
#import "BlinkingColors.h" | |
@interface BlinkingLabelOperation () | |
- (void)updateLabel; | |
@end | |
@implementation BlinkingLabelOperation | |
- (id)init { | |
[self release]; | |
@throw [NSException exceptionWithName:NSInternalInconsistencyException | |
reason:@"-init is not a valid initializer for the class BlinkingLabelOperation" | |
userInfo:nil]; | |
return nil; | |
} | |
// default initializer | |
- (id)initWithLabel:(UILabel *)label freq:(NSTimeInterval)interval normalColors:(BlinkingColors *)normal blinkColors:(BlinkingColors *)blink { | |
self = [super init]; | |
if (self) { | |
mode_ = YES; | |
interval_ = interval; | |
normalColors_ = [normal retain]; | |
blinkColors_ = [blink retain]; | |
// Dont retain the label, because if the view unloads | |
// the operation stop | |
label_ = label; | |
} | |
return self; | |
} | |
- (id)initWithLabel:(UILabel *)label freq:(NSTimeInterval)interval blinkColors:(BlinkingColors *)blink { | |
// get the current color of label as the normal color | |
BlinkingColors *normal = [[BlinkingColors alloc] initWithBackgroundColor:label.backgroundColor | |
textColor:label.textColor]; | |
// call the designated initializer | |
return [self initWithLabel:label freq:interval | |
normalColors:normal blinkColors:blink]; | |
} | |
- (void)main { | |
SEL update = @selector(updateLabel); | |
[self setThreadPriority:0.0]; | |
while (![self isCancelled]) { | |
if (label_ == nil) | |
break; | |
[NSThread sleepForTimeInterval:interval_]; | |
[self performSelectorOnMainThread:update withObject:nil waitUntilDone:YES]; | |
} | |
} | |
- (void)updateLabel { | |
BlinkingColors *currentColors = nil; | |
if (mode_) | |
currentColors = blinkColors_; | |
else | |
currentColors = normalColors_; | |
[label_ setTextColor:currentColors.textColor]; | |
[label_ setBackgroundColor:currentColors.backgroundColor]; | |
mode_ = !mode_; | |
} | |
- (void)dealloc { | |
[blinkColors_ release]; | |
[normalColors_ release]; | |
// don't release the label because I never owned it | |
[super dealloc]; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment