本文介绍了iOS动画UILabel展开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UILabel,它以3行文本开头:

I have a UILabel that's initiated with 3 lines of text:

locationDescription = [[UILabel alloc]init];
locationDescription.numberOfLines = 3;
[locationDescription setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:locationDescription];

然后我有一个扩展标签的按钮:

I then have a button that expands the label:

- (void)buttonPressed:(UIButton *)sender{
    NSLog(@"Pressed");
    [UIView animateWithDuration:1
                     animations:^{locationDescription.numberOfLines = 0;}
     ];
}

标签的理想行为是让每条多余的线在一个时间。标签可以很好地扩展,但是过渡效果却不理想,所有行都可以立即显示。

The desired behavior for the label is to have each extra line reveal itself one at a time. The label expands fine, but the transition isn't animated and all the lines just show up at once.

我想念的是什么?

推荐答案

您可以设置行数的动画。它更改 UILabel 的内在大小。您需要在包含UILabel的视图上的动画块内调用layoutIfNeeded。在这里,我将点击手势附加到表格上,以在具有0(任意数量)行和1行之间切换。

You can animate number of lines. It changes the intrinsicSize of the UILabel. You need to call layoutIfNeeded inside your animation block on the view containing the UILabel. Here I attach a tap gesture to the table to toggle it between having 0 (as many as you want) lines and 1 line.

 func tapLabel(tapGesture: UITapGestureRecognizer)
 {
    if let label = tapGesture.view as? UILabel {
      label.numberOfLines = label.numberOfLines == 0 ? 1 : 0
      UIView.animateWithDuration(0.5) {
        label.superview?.layoutIfNeeded()
      }
    }
 }

这篇关于iOS动画UILabel展开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:40