iOS动画UILabel扩展

she*_*eep 11 core-animation uilabel ios

我有一个用3行文本启动的UILabel:

locationDescription = [[UILabel alloc]init];
locationDescription.numberOfLines = 3;
[locationDescription setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addSubview:locationDescription];
Run Code Online (Sandbox Code Playgroud)

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

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

标签的期望行为是让每个额外的行一次显示一个.标签扩展正常,但转换不是动画,所有行只是立即显示.

我错过了什么?

Ray*_*Fix 16

您可以为行数设置动画.它改变了内在的大小UILabel.您需要在包含UILabel的视图中的动画块内调用layoutIfNeeded.在这里,我将一个轻击手势附加到表格,以在具有0(尽可能多的)行和1行之间切换.

 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()
      }
    }
 }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 通过"查看"到"顶部"下的"内容模式",它将平滑地显示隐藏的线条...... (22认同)