Blinking UILabel Cocoa Touch
Is it possible to make a blinking UILabel in Cocoa Touch o开发者_Python百科r do I need an UIview with Core Animation for that?
Take Martin's advice, and then have a look at NSTimer to handle the "blink" actions.
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
All UIViews (including UILabel) has a hidden
property which you can toggle on and off to make it "blink".
For fun, I decided to write this subclassing NSOperation.
Excerpt from BlinkingLabelOperation.m
- (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_;
}
Sample view controller code:
- (void)viewDidLoad
{
[super viewDidLoad];
BlinkingColors *blinkColors = [[BlinkingColors alloc] initWithBackgroundColor:[UIColor whiteColor]
textColor:[UIColor redColor]];
BlinkingLabelOperation *blinkingOp = [[BlinkingLabelOperation alloc] initWithLabel:clickLabel freq:1.0 blinkColors:blinkColors];
// put the operation on a background thread
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:blinkingOp];
[blinkColors release];
}
For a complete listing, you will find it here. Please leave comments and let me know what are your thoughts.
精彩评论