我实际上遇到了在iOS应用程序中设置UILabel动画的问题。 在网络上搜索代码片段2天后,仍然没有结果。
我发现的每个样本都是关于如何为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];
但是Pulse或缩放动画呢?
答案 0 :(得分:13)
不幸的是,字体大小不是NSView的可动画属性。为了扩展UILabel,您需要使用更高级的Core Animation技术,使用CAKeyframeAnimation:
#import <QuartzCore/QuartzCore.h>
。setValues
方法将转换添加到CAKeyframeAnimation对象,使转换成为动画中的关键帧之一。setDuration
方法[[yourLabelObject layer] addAnimation:yourCAKeyframeAnimationObject forKey:@"anyArbitraryString"
] 最终代码可能如下所示:
// 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"];
我将把重复的“脉冲”动画作为练习留给你:提示,它涉及animationDidStop
方法!
另一个注意事项 - 可以找到CALayer动画属性的完整列表(其中“transform”为1)here。快乐的补间!