闪烁的 UILabel Cocoa Touch

发布于 2024-11-05 01:44:58 字数 66 浏览 2 评论 0原文

是否可以在 Cocoa Touch 中制作一个闪烁的 UILabel,或者我是否需要一个带有核心动画的 UIview?

Is it possible to make a blinking UILabel in Cocoa Touch or do I need an UIview with Core Animation for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沫尐诺 2024-11-12 01:44:58

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

+ ScheduledTimerWithTimeInterval:目标:选择器:userInfo:重复:

Take Martin's advice, and then have a look at NSTimer to handle the "blink" actions.

+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

一指流沙 2024-11-12 01:44:58

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

All UIViews (including UILabel) has a hidden property which you can toggle on and off to make it "blink".

枫林﹌晚霞¤ 2024-11-12 01:44:58

为了好玩,我决定编写这个 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];
}

有关完整列表,您可以在此处找到它。请发表评论并让我知道您的想法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文