public
Created

Blinking Label Operation

  • Download Gist
BlinkingColors.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//
// 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
BlinkingColors.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
//
// 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
BlinkingLabelOperation.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
//
// 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
BlinkingLabelOperation.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
//
// 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.