在 iOS 应用程序中使用 CoreAnimation / QuartzCore 动画 UILabel

发布于 2024-12-05 18:25:02 字数 996 浏览 4 评论 0原文

实际上,我遇到了在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-12-12 18:25:02

不幸的是,字体大小不是 NSView 的动画属性。为了缩放 UILabel,您需要使用更高级的 核心动画技术,使用CAKeyframeAnimation

  1. 将 QuartzCore.framework 导入到您的项目,并在代码中#import
  2. 创建一个新的 CAKeyframeAnimation 对象您可以将关键帧添加到。
  3. 创建一个 CATransform3D 值来定义缩放操作(不要对 3D 部分感到困惑——您可以使用此对象在图层上进行任何转换)。
  4. 通过将变换添加到 CAKeyframeAnimation 对象使用其 setValues 方法。
  5. 通过调用其 setDuration 方法设置动画的持续时间
  6. 最后,使用 [[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 可动画属性的完整列表(其中“变换”是其中之一)可以在 此处。快乐补间!

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:

  1. Import the QuartzCore.framework into your project, and #import <QuartzCore/QuartzCore.h> in your code.
  2. Create a new CAKeyframeAnimation object that you can add your key frames to.
  3. Create a CATransform3D value defining the scaling operation (don't get confused by the 3D part--you use this object to do any transformations on a layer).
  4. Make the transformation one of the keyframes in the animation by adding it to the CAKeyframeAnimation object using its setValues method.
  5. Set a duration for the animation by calling its setDuration method
  6. Finally, add the animation to the label's layer using [[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!

One other note--the full list of CALayer animatable properties (of which "transform" is one) can be found here. Happy tweening!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文