【问题标题】:How can I animate the size of UILabel or UITextField text?如何为 UILabel 或 UITextField 文本的大小设置动画?
【发布时间】:2018-06-05 06:11:36
【问题描述】:

我在 UIView 中有一个 UITextfield,其中包含一些默认文本。我想为这个 textField(连同父 UIView)%300 的缩放设置动画,然后再回到 100%。

我该怎么做?似乎我发现的所有内容都涉及到 hacky 图像捕获和 CGAffineTransformMakeScale。

我花了很多时间来搜索这个,要么很难和/或成本高昂,要么实现起来非常简单。如果是后者,我不会感到惊讶。 :)

【问题讨论】:

  • 你想改变框架的大小吗?

标签: ios objective-c uilabel core-animation


【解决方案1】:

您不必从视图中捕获图像,您只需使用以下代码在标签上使用缩放变换

[UIView beginAnimations:nil context:nil];
label.transform = CGAffineTransformMakeScale(3.0, 3.0);
[UIView commitAnimations];

但这种方法的唯一问题是文本可能看起来不清晰。

如果您希望UILabel 的缩放比我建议您尝试核心文本框架更清晰。这肯定会帮助您实现目标。

同样的教程可以在here找到。

【讨论】:

  • 您现在可以为 UIView 动画使用块:[UIView animateWithDuration:0.5f animations:^{ label.transform = CGAffineTransformMakeScale(3.0, 3.0); }]
【解决方案2】:

参考:Animations-Explained

ios/documentation/CoreAnimation_guide

CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position.x";
animation.values = @[ @0, @10, @-10, @10, @0 ];
animation.keyTimes = @[ @0, @(1 / 6.0), @(3 / 6.0), @(5 / 6.0), @1 ];
animation.duration = 0.4;

animation.additive = YES;

[form.layer addAnimation:animation forKey:@"shake"];

iOS - Animating text size change in UILabel or UITextView?

这可能对你有帮助:)

【讨论】:

    【解决方案3】:

    在 Swift 4.1 中

    使用 UIView.animate(withDuration:) 不适用于 FontSize。并且使用CGAffineTransform很难管理,必须应用于UILabelUITextField,但正如接受的回复中所说,结果并不清晰。

    这是我正在使用的解决方案。

    var fontSize: CGFloat = 12.0
    Timer.scheduledTimer(withTimeInterval: 0.02, repeats: true) { (timer) in
        self.font = UIFont(name: "Roboto-Regular", size: fontSize)
        fontSize += 0.5 // Or 0.333 to allow for 3X resolution screens.
        if fontSize > 17.0 {
            timer.invalidate()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多