【问题标题】:Animating UILabel size decrease动画UILabel大小减小
【发布时间】:2012-10-25 18:15:06
【问题描述】:

增加标签高度时,一切都很好。减小时,标签会立即改变大小,然后通过动画重新定位。

@interface
@property (nonatomic, retain) IBOutlet UILabel *explanationLabel;

@implementation
CGRect frmExpl = explanationLabel.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.75];

frmExpl.size.height -= height;
explanationLabel.frame = frmExpl;

[UIView commitAnimations];  

我试过用 UIView 替换 UILabel,当然 UIView 没有这样的问题。

有什么特殊的方法可以让 UILabel 尺寸减小吗?

这是一个演示所描述问题的最小项目。 Download

【问题讨论】:

标签: iphone animation


【解决方案1】:

问题在于 UILabel 会在其大小发生变化时重新绘制自身。 (它不能重绘动画的每一帧,因为文本渲染发生在 CPU 上,而不是运行 UIView 动画的 GPU 上。)您可以通过将标签的 contentMode 属性更改为例如 UIViewContentModeCenter 来阻止它重绘。

【讨论】:

    【解决方案2】:

    我认为您要更改的是边界,而不是框架。来自文档:

    “边界矩形确定其框架矩形内视图坐标系中的原点和比例,并以点为单位。设置此属性会相应地更改框架属性的值。” - UIView 类;边界属性

    尝试类似:

    - (void)animate:(id)sender
    {
        ...
        CGRect newBounds = testLabel.bounds;
        newBounds.size.height += 50;
        testLabel.bounds = newBounds;
        ...
    }
    

    【讨论】:

    • 增加边界确实会改变框架,但框架在屏幕上保持不变。递减界限有效。但这不是解决方案,因为我需要增加和减少。我怀疑这是框架中的错误。
    • 没错,这可能是一个错误。您能否将视图层次结构更改为具有蓝色背景的 UIView,然后是固定大小的 UILabel,它仅将其支柱和弹簧设置为浮动在 UIView 的中间。鉴于 UIView 动画正确,这可能是一个解决方案。
    【解决方案3】:

    使用CGAffineTransform 来执行此操作。

    [UIView animateWithDuration:1.0 animations:^{
        // Scale down 50%
        label.transform = CGAffineTransformScale(label.transform, 0.5, 0.5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 animations:^{
            // Scale up 50%
            label.transform = CGAffineTransformScale(label.transform, 2, 2);
        }];
    }];
    

    【讨论】:

    • 它对我有用,但它会影响其他动画,如淡入和淡出 UIView。你能告诉我如何解决这个问题以及为什么这些动画会发生冲突。我对你的答案投了赞成票。
    • 在 UILables 上使用仿射变换确实有一些丑陋的副作用,比如扭曲标签上的文本。不是一个有用的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    相关资源
    最近更新 更多