在 iOS 应用程序中使用 CoreAnimation / QuartzCore 动画 UILabel
实际上,我遇到了在 iOS 应用程序中对 UILabel 进行动画处理的问题。 在网上搜索代码片段两天后,仍然没有结果。
我找到的每个示例都是关于如何对 UIImage 进行动画处理,将其作为子视图逐层添加到 UIView 中。有没有关于 UILabel 动画的好例子? 我通过设置 alpha 属性找到了一个很好的闪烁动画解决方案,如下所示:
我的函数:
- (void)blinkAnimation:(NSString *)animationID finished:(BOOL)finished target:(UIView *)target
{
NSString *selectedSpeed = [[NSUserDefaults standardUserDefaults] stringForKey:@"EffectSpeed"];
float speedFloat = (1.00 - [selectedSpeed floatValue]);
[UIView beginAnimations:animationID context:target];
[UIView setAnimationDuration:speedFloat];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(blinkAnimation:finished:target:)];
if([target alpha] == 1.0f)
[target setAlpha:0.0f];
else
[target setAlpha:1.0f];
[UIView commitAnimations];
}
在 UILabel 上调用我的函数:
[self blinkAnimation:@"blinkAnimation" finished:YES target:labelView];
但是脉冲或缩放动画怎么样?
I actually stuck on a problem with animating a UILabel in my iOS Application.
After 2 days of searching the web for code snippets, still no result.
Every sample I found was about how to animate UIImage, adding it as a subview to UIView by layer. Is there any good example about animating a UILabel?
I found a nice solution for a blinking animation by setting the alpha property, like this:
My function:
- (void)blinkAnimation:(NSString *)animationID finished:(BOOL)finished target:(UIView *)target
{
NSString *selectedSpeed = [[NSUserDefaults standardUserDefaults] stringForKey:@"EffectSpeed"];
float speedFloat = (1.00 - [selectedSpeed floatValue]);
[UIView beginAnimations:animationID context:target];
[UIView setAnimationDuration:speedFloat];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(blinkAnimation:finished:target:)];
if([target alpha] == 1.0f)
[target setAlpha:0.0f];
else
[target setAlpha:1.0f];
[UIView commitAnimations];
}
Call my function on the UILabel:
[self blinkAnimation:@"blinkAnimation" finished:YES target:labelView];
But how about a Pulse, or scaling animation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,字体大小不是 NSView 的动画属性。为了缩放 UILabel,您需要使用更高级的 核心动画技术,使用CAKeyframeAnimation:
#import
。setValues
方法。setDuration
方法设置动画的持续时间[[yourLabelObject layer] addAnimation:yourCAKeyframeAnimationObject forKey:@"anyArbitraryString"
] 将动画添加到标签的图层最终的代码可能如下所示:
我将保留重复的“脉冲”动画作为练习:提示,它涉及
animationDidStop
方法!另请注意 - CALayer 可动画属性的完整列表(其中“变换”是其中之一)可以在 此处。快乐补间!
Unfortunately font size is not an animatable property of NSView. In order to scale a UILabel, you'll need to use more advanced Core Animation techniques, using CAKeyframeAnimation:
#import <QuartzCore/QuartzCore.h>
in your code.setValues
method.setDuration
method[[yourLabelObject layer] addAnimation:yourCAKeyframeAnimationObject forKey:@"anyArbitraryString"
]The final code could look something like this:
I'll leave the repeating "pulse" animation as an exercise for you: hint, it involves the
animationDidStop
method!One other note--the full list of CALayer animatable properties (of which "transform" is one) can be found here. Happy tweening!