在iOS App中使用CoreAnimation / QuartzCore动画UILabel

我实际上在我的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或缩放动画呢?

解决方法

不幸的是,字体大小不是NSView的可动画属性.为了扩展UILabel,您需要使用更高级的 Core Animation技术,使用 CAKeyframeAnimation

>将QuartzCore.framework导入项目,#import< QuartzCore / QuartzCore.h>在你的代码中.
>创建一个可以添加关键帧的新CAKeyframeAnimation对象.
>创建一个定义缩放操作的CATransform3D值(不要被3D部分混淆 – 您使用此对象在图层上进行任何变换).
>通过使用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方法

一个注意事项 – 可以在here找到CALayer动画属性的完整列表(其中“transform”是一个).快乐的补间!

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...