IOS animation UILabel expands

I have a UILabel that starts with 3 lines of text:

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

Then I have a button that extends the label:

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

The desired label behavior is for each additional line to show itself one at a time. The label expands fine, but the transition is not animated, and all lines appear immediately.

What am I missing?

+8
ios uilabel core-animation
source share
3 answers

You can revive the number of lines. It changes the internal value of UILabel . You need to call layoutIfNeeded inside your animation block in the view containing the UILabel. Here I attach a binding gesture to the table to switch it between having 0 (as many as you want) rows and 1 row.

  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() } } } 

enter image description here

+12
source share

You cannot animate the number of lines, you must animate the size and set the number of lines to 0 from the very beginning.

I am not adding height calculation code since it hides the real animation here.

 self.locationDescription = [[UILabel alloc] init]; self.locationDescription.numberOfLines = 0; [locationDescription setTranslatesAutoresizingMaskIntoConstraints:NO]; // Say, it starts with 50. In your real code, height should be calculated // to fit the size you want, rounded to lines NSDictionary *viewsDict = @{@"locationDescription": self.locationDescription}; [self.locationDescription addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:[locationDescription(50)]" options:0 metrics:nil views:viewsDict]; 

Then in action do

 - (void)buttonPressed:(UIButton *)sender { NSLog(@"Pressed"); // Assuming there is only one constraint. If not, assign it to another property // I put 500, but, again, real size rounded to line height should be here self.locationDescription.constraints[0].constant = 500; [UIView animateWithDuration:1 animations:^{ // Layouting superview, as its frame can be updated because of a new size [self.locationDescription.superview layoutIfNeeded]; }]; } 

In addition, you must assign a locationDescription to the property and access it with self. in front.

+2
source share

The UIView animateWithDuration object contains changes to commit to views. Here you programmatically change any animated properties of the views in the view hierarchy. But the property you specified, numberOfLines, is not an animated property.

The following properties of the UIView class, which is the UILabel parent class, are animated:

  @property frame @property bounds @property center @property transform @property alpha @property backgroundColor @property contentStretch 
+2
source share

All Articles