fuz*_*oat 14 iphone cocoa-touch objective-c
我试图为UILabel上的文字设置颜色的动画来从:[黑色]到[白色]到[黑色]并重复.
- (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];
}
Run Code Online (Sandbox Code Playgroud)
.
[self setFadeTimer:[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFlash:) userInfo:nil repeats:YES]];
Run Code Online (Sandbox Code Playgroud)
首先,我不确定我的方法,我的计划(如上所示)是设置动画块并使用重复的NSTimer调用它直到取消.
我的第二个问题(正如你在上面看到的)是我从黑色(alpha 0)到白色(alpha 1)的动画,但我不知道如何再次动画回黑色,所以动画无缝循环
基本上我想要的是在UILabel上脉冲的文本颜色,直到用户按下按钮继续.
我遇到了麻烦,因为你不能动画[UILabel setColor:]你可以动画[UILabel setAlpha:]所以我要去试试.
- (void)timerFlash:(NSTimer *)timer {
[[self navTitle] setAlpha:0.5];
[UIView animateWithDuration:2
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{[[self navTitle] setAlpha:0.9];}
completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
这是有效的(顺便说一句:我确实希望它停止,这就是为什么我把它连接到NSTimer,所以我可以取消它)唯一的事情就是这从midGray到nearWhite的动画然后弹出.有谁知道我会如何动画从nearWhite到midGray,所以我得到了一个很好的平滑循环?

Dave DeLong(见下文)建议的代码在修改为使用CALayer不透明度样式属性时确实有效:
UILabel *navTitle;
@property(nonatomic, retain) UILabel *navTitle;
Run Code Online (Sandbox Code Playgroud)
.
// 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"];
Run Code Online (Sandbox Code Playgroud)
.
// REMOVE ANIMATION
[[[self navTitle] layer] removeAnimationForKey:@"flash"];
Run Code Online (Sandbox Code Playgroud)
Dav*_*ong 15
你可以用一个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];
Run Code Online (Sandbox Code Playgroud)
你fromValue和你的toValue值会是CGColorRefs,你的动画密钥路径会有所不同,但这里的诀窍是setAutoreverses:YES.这将从动画fromValue到动画toValue,然后再动画到动画fromValue.它非常方便.:)
https://github.com/davedelong/Demos/blob/master/OlympicRings
如果这不起作用(并且它可能不起作用),那么我可能只是将第二层叠UILabels在一起,并同时将它们的alpha值从0.0设置为1.0(反之亦然).
如果你只想让它无限期地发生,你可以使用Dave DeLong的答案,或者:
[UIView animateWithDuration:1
delay:0
options:UIViewAnimationOptionAllowUserInteraction |
UIViewAnimationOptionAutoreverse |
UIViewAnimationOptionRepeat
animations:^{[[self navTitle] setTextColor:[UIColor whiteColor]];}
completion:nil];
Run Code Online (Sandbox Code Playgroud)
当然,使用CAAnimation的好处是可以在以后删除动画; 有了这个,为了阻止你,你必须深入了解CALayer并找到动画来阻止它.
但是,您建议UILabel.textColor可能无法生成动画.这是可能的.如果是这样,您可以使用相同的技术在两个不同颜色的UILabel之间进行转换,然后淡化它们的alpha值.
| 归档时间: |
|
| 查看次数: |
8946 次 |
| 最近记录: |