iphone - 为脉动的 UILabel 制作动画?

标签 iphone objective-c cocoa-touch

我正在尝试为 UILabel 上的文本设置动画颜色,使其从 [Black] 到 [White] 再到 [Black] 并重复。

- (void)timerFlash:(NSTimer *)timer {
[[self navTitle] setTextColor:[[UIColor whiteColor] colorWithAlphaComponent:0.0]];
[UIView animateWithDuration:1
                      delay:0 
                    options:UIViewAnimationOptionAllowUserInteraction 
                 animations:^{[[self navTitle] setTextColor:[[UIColor whiteColor] colorWithAlphaComponent:1.0]];} 
                 completion:nil];
}

.

[self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFlash:) userInfo:nil repeats:YES]];

首先我不确定我的方法,我的计划(如您在上面看到的)是设置一个动画 block 并使用重复的 NSTimer 调用它直到取消。

我的第二个问题(正如您在上面看到的)是我正在从黑色(alpha 0)到白色(alpha 1)制作动画,但我不知道如何再次制作回黑色动画,以便动画无缝循环

本质上,我想要的是文本颜色在 UILabel 上闪烁,直到用户按下按钮继续。

EDIT_001:

我遇到了麻烦,因为你不能为 [UILabel setColor:] 设置动画,但是你可以为 [UILabel setAlpha:] 设置动画,所以我要试一试。

EDIT_002:

- (void)timerFlash:(NSTimer *)timer {
    [[self navTitle] setAlpha:0.5];
    [UIView animateWithDuration:2 
                          delay:0 
                        options:UIViewAnimationOptionAllowUserInteraction 
                     animations:^{[[self navTitle] setAlpha:0.9];} 
                     completion:nil];
}

这有效(顺便说一句:我确实希望它停止,这就是为什么我将它连接到 NSTimer 以便我可以取消它)唯一的事情是它从 midGray 到 nearWhite 动画然后弹回。有谁知道我如何将动画从 nearWhite 返回到 midGray 以便我获得一个很好的平滑循环? enter image description here

EDIT_003:(解决方案)

Dave DeLong 建议的代码(见下文)在修改为使用 CALayer 不透明度样式属性时确实有效:

UILabel *navTitle;
@property(nonatomic, retain) UILabel *navTitle;

.

// ADD ANIMATION
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"opacity"];
[anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[anim setFromValue:[NSNumber numberWithFloat:0.5]];
[anim setToValue:[NSNumber numberWithFloat:1.0]];
[anim setAutoreverses:YES];
[anim setDuration:0.5];
[[[self navTitle] layer] addAnimation:anim forKey:@"flash"];

.

// REMOVE ANIMATION
[[[self navTitle] layer] removeAnimationForKey:@"flash"];

最佳答案

您可能可以使用 CAAnimation 来做到这一点。我刚刚把我前一段时间做的一个小演示项目推到了github上。它显示奥林匹克标志,并让圆环飞到屏幕上的随机位置,然后动画回到原来的位置。您可以可能做一些非常相似的事情,但显然不使用 position 属性。

下面是让其中一个环飞来飞去的基本代码:

CABasicAnimation * a = [CABasicAnimation animationWithKeyPath:@"position"];
[a setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[a setFromValue:[NSValue valueWithCGPoint:[layer position]]];
[a setToValue:[NSValue valueWithCGPoint:[self randomPoint]]];
[a setAutoreverses:YES];
[a setDuration:1.0];
[layer addAnimation:a forKey:nil];

您的 fromValuetoValue 值将是 CGColorRefs,并且您的动画键路径会有所不同,但这里的技巧是 设置自动反转:是。这将从 fromValue 动画到 toValue,然后返回到 fromValue。这非常方便。 :)

https://github.com/davedelong/Demos/blob/master/OlympicRings


如果那行不通(也可能行不通),那么我可能只是将两个 UILabels 层叠在一起,并将它们的 alpha 值设置为从 0.0 到 1.0(或反之亦然)的动画同时。

关于iphone - 为脉动的 UILabel 制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5035120/

相关文章:

iphone - NSDate 对于特定日期为 null

iphone - 选择 UITableViewCell AccessoryView,与选择行分开

objective-c - 将 UITable 中的 UITableViewCell 中的 subview 中的对象的 CGRect 转换为相对于 self.view 的 CGRect

javascript - 排除 iOS 运行 JavaScript

iphone - 如何在Cocos2d-x(C++ openGL)中制作可拉伸(stretch)图像

iphone - 试图将 long long 从 String 保存到 NSNumber

ios - IBOutletCollection 中不同对象的框架返回空

iphone - 设置 body 后如何设置 NSMutableRequest 超时小于 240 秒?

objective-c - 带回调的 CAAnimation

ios - 使用 sendAsynchronousRequest :queue:completionHandler: 处理 401 响应