用UILabel调整UIView大小并正确进行动画

4

我有一个包含UILabel的UIView容器,就像这样:

-------------       ---------
|           |       |       |
|Label text |       |Label..|
|           |  -->  |       |
|           |       |       |
|           |       |       |
-------------       ---------

现在我使用:UIView animateWithDuration:animations:,并尝试将包含UILabel的UIView的宽度设置为较小值时,在动画期间UILabel突然被替换为“...”,而没有平滑过渡。我将UILabel的autoresizingmask设置为UIViewAutoresizingFlexibleWidth、UIViewAutoresizingFlexibleRightMargin以保持其左侧,并将contentmode设置为left。尝试其他内容模式例如:Scale to fill,Aspect fit,Aspect fill也不能解决我的问题,因为它们会缩放文本,使其看起来很奇怪。有人有想法如何让UILabel平滑过渡吗?
4个回答

5

我一段时间前用描述的方法给我的标签添加了动画,但你需要付出一些努力。 基本思路: - 将标签添加到带有 clipsToBounds:YES 的 containerview 中。 - 不要动画化标签框架,而是动画化 containerview 的框架,这样您的标签将具有平滑过渡。 - 使用单独的 Label 为省略号(...),以便您也可以动画化此部分。


0

虽然这是一个老问题,但它对我很有帮助:

    override var frame: CGRect {
        didSet {
            self.layoutSubviews()
        }
    }

0

很遗憾,标签不会自动缩放;更改其框架会更改文本的位置,但文本的大小不会以这种方式缩放。因此,基本动画不可行,另一种选择是启动一个计时器,每次计时器触发时将标签的框架缩小预定的量。

当然,您需要将标签的setAdjustsFontSizeToFitWidth属性设置为YES

注意:如果可能,请避免每5/1000秒实际触发计时器。

- (void)fireTimer
{
    timer = [NSTimer scheduledTimerWithTimeInterval:0.005 target:self selector:@selector(animate) userInfo:nil repeats:YES];
}

- (void)animate
{
    [label setAdjustsFontSizeToFitWidth:YES];
    if (label.frame.size.width > 100) {
        [label setFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width-1, label.frame.size.height)];
    }else{
        [timer invalidate];
        timer = nil;
    }
}

1
如前所述,您应避免频繁触发计时器,但是您可以使用CADisplayLink,它是一种以显示刷新率触发的计时器。请注意,由于没有固定的时间间隔,您必须计算自上次调用以来经过的时间。 - Jan Christiansen
@JanChristiansen 很好知道,我甚至不知道那个存在! - Mick MacCallum
它似乎并不那么出名。当我想要为setContentOffset动画实现一个非线性时间函数的UIScrollView时,我偶然发现了它。 - Jan Christiansen

0
我在自动旋转期间调整UILabel大小时遇到了类似的问题,通过使用CADisplayLink解决了它。
首先,在willAnimateRotationToInterfaceOrientation方法中调用displayLinkTimer。
displayLinkTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(resizeTextLabel)];
[displayLinkTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

然后,通过按照我创建的名为textView的不可见UIView的大小比例来调整标签的大小。 textView的框架与我想要的UILabel的框架相同,并且它的大小在willAnimateRotationToInterfaceOrientation方法中被更改。 通过访问textView的presentationLayer,我能够以相同的速度调整大小。
- (void)resizeTextLabel{
 if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
    if (textLabel.frame.size.width < textView.frame.size.width -20) {
        textLabel.frame = CGRectMake(0, 0, [[textView.layer presentationLayer] frame].size.width, [[textView.layer presentationLayer] frame].size.height);
    }else{
        [timer invalidate];
        timer = nil;
    }
 }
 else{
    if (textLabel.frame.size.width > textView.frame.size.width -20) {
        textLabel.frame = CGRectMake(0, 0, [[textView.layer presentationLayer] frame].size.width, [[textView.layer presentationLayer] frame].size.height);
    }else{
        [timer invalidate];
        timer = nil;
    }
  }
}

希望能对某人有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接