【问题标题】:Blinking UILabel Cocoa Touch闪烁的 UILabel Cocoa Touch
【发布时间】:2011-05-03 21:24:05
【问题描述】:

是否可以在 Cocoa Touch 中制作一个闪烁的 UILabel,或者我需要一个带有 Core Animation 的 UIview 吗?

【问题讨论】:

  • 警告! 如果用户界面元素以特定频率闪烁,则闪烁的用户界面元素可能会引发癫痫发作。实现此类动画时要小心。

标签: iphone objective-c cocoa-touch uilabel


【解决方案1】:

听从 Martin 的建议,然后看看 NSTimer 来处理“眨眼”动作。

+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

【讨论】:

  • 如果你想变得花哨,你可以用 alpha 值打开和关闭动画。
  • 只有3秒,显示暂停的音频播放器状态。没有什么有害的。谢谢。
【解决方案2】:

所有 UIView(包括 UILabel)都有一个 hidden 属性,您可以打开和关闭它以使其“闪烁”。

【讨论】:

  • 马丁,感谢您指出这一点。在这一点上,只有一个 UILabel 适合我的需求。
【解决方案3】:

为了好玩,我决定编写这个子类化 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,让我知道您的想法。

【讨论】:

  • 嗨,Black Frog,非常感谢您的回答和您的代码!非常好,易于实施,效果很好,正是我想要的。两个竖起大拇指:-)
猜你喜欢
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多