xcode 在 iOS 应用程序中使用 CoreAnimation / QuartzCore 为 UILabel 设置动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7502098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Animating UILabel with CoreAnimation / QuartzCore in iOS App
提问by DevZarak
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.
我实际上遇到了在我的 iOS 应用程序中为 UILabel 设置动画的问题。在网上搜索代码片段 2 天后,仍然没有结果。
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:
我发现的每个示例都是关于如何为 UIImage 设置动画,将其作为子视图逐层添加到 UIView。有没有关于动画 UILabel 的好例子?我通过设置 alpha 属性找到了一个很好的闪烁动画解决方案,如下所示:
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:
在 UILabel 上调用我的函数:
[self blinkAnimation:@"blinkAnimation" finished:YES target:labelView];
But how about a Pulse, or scaling animation?
但是脉冲或缩放动画呢?
回答by sgress454
Unfortunately font size is not an animatable property of NSView. In order to scale a UILabel, you'll need to use more advanced Core Animationtechniques, using CAKeyframeAnimation:
不幸的是,字体大小不是 NSView 的动画属性。为了缩放 UILabel,您需要使用更高级的核心动画技术,使用CAKeyframeAnimation:
- Import the QuartzCore.framework into your project, and
#import <QuartzCore/QuartzCore.h>
in your code. - Create a new CAKeyframeAnimationobject that you can add your key frames to.
- Create a CATransform3Dvalue defining the scaling operation (don't get confused by the 3D part--you use this object to do anytransformations on a layer).
- Make the transformation one of the keyframes in the animation by adding it to the CAKeyframeAnimationobject using its
setValues
method. - Set a duration for the animation by calling its
setDuration
method - Finally, add the animation to the label's layer using
[[yourLabelObject layer] addAnimation:yourCAKeyframeAnimationObject forKey:@"anyArbitraryString"
]
- 将 QuartzCore.framework 导入到您的项目和
#import <QuartzCore/QuartzCore.h>
代码中。 - 创建一个新的CAKeyframeAnimation对象,您可以向其中添加关键帧。
- 创建一个定义缩放操作的CATransform3D值(不要被 3D 部分弄糊涂——你使用这个对象在一个层上做任何变换)。
- 通过使用其方法将转换添加到CAKeyframeAnimation对象,使转换成为动画中的关键帧之一
setValues
。 - 通过调用其
setDuration
方法设置动画的持续时间 - 最后,使用
[[yourLabelObject layer] addAnimation:yourCAKeyframeAnimationObject forKey:@"anyArbitraryString"
]将动画添加到标签层
The final code could look something like this:
最终代码可能如下所示:
// Create the keyframe animation object
CAKeyframeAnimation *scaleAnimation =
[CAKeyframeAnimation animationWithKeyPath:@"transform"];
// Set the animation's delegate to self so that we can add callbacks if we want
scaleAnimation.delegate = self;
// Create the transform; we'll scale x and y by 1.5, leaving z alone
// since this is a 2D animation.
CATransform3D transform = CATransform3DMakeScale(1.5, 1.5, 1); // Scale in x and y
// Add the keyframes. Note we have to start and end with CATransformIdentity,
// so that the label starts from and returns to its non-transformed state.
[scaleAnimation setValues:[NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DIdentity],
[NSValue valueWithCATransform3D:transform],
[NSValue valueWithCATransform3D:CATransform3DIdentity],
nil]];
// set the duration of the animation
[scaleAnimation setDuration: .5];
// animate your label layer = rock and roll!
[[self.label layer] addAnimation:scaleAnimation forKey:@"scaleText"];
I'll leave the repeating "pulse" animation as an exercise for you: hint, it involves the animationDidStop
method!
我将把重复的“脉冲”动画留给你作为练习:提示,它涉及animationDidStop
方法!
One other note--the full list of CALayer animatable properties (of which "transform" is one) can be found here. Happy tweening!
另一个注意事项 - 可以在此处找到 CALayer 可动画属性的完整列表(“变换”就是其中之一)。补间快乐!