【发布时间】:2011-05-03 21:24:05
【问题描述】:
是否可以在 Cocoa Touch 中制作一个闪烁的 UILabel,或者我需要一个带有 Core Animation 的 UIview 吗?
【问题讨论】:
-
警告! 如果用户界面元素以特定频率闪烁,则闪烁的用户界面元素可能会引发癫痫发作。实现此类动画时要小心。
标签: iphone objective-c cocoa-touch uilabel
是否可以在 Cocoa Touch 中制作一个闪烁的 UILabel,或者我需要一个带有 Core Animation 的 UIview 吗?
【问题讨论】:
标签: iphone objective-c cocoa-touch uilabel
听从 Martin 的建议,然后看看 NSTimer 来处理“眨眼”动作。
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
【讨论】:
所有 UIView(包括 UILabel)都有一个 hidden 属性,您可以打开和关闭它以使其“闪烁”。
【讨论】:
为了好玩,我决定编写这个子类化 NSOperation。
摘自 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_;
}
示例视图控制器代码:
- (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];
}
如需完整列表,您可以找到here。请留下 cmets,让我知道您的想法。
【讨论】: