Animation of change constraints, position animation, but height transitions without animation

I have a custom UIView. Inside this UIView, I have a UILabel with a yellow background containing @ "8" under the name labelWhite. I programmatically create 4 restrictions for this UILabel: top, left, width and height.

When I click UIView, I change the constant height value and animate the layout for 5 seconds. However, the presentation height immediately jumps to the new value, but the y position also changes and immediately moves to the new value. Then, during the 5-second animation, the y position comes back to where it was supposed to stay all the time.

Here you can see the video: http://inadaydevelopment.com/stackoverflow/Constraints0.mov

What this SHOULD do is simply remain at y = 0 and decrease vertically. What am I doing wrong?


EDIT:

I just found that my animations work exactly as intended if my routines are UIViews, but as UILabels I get the transition size + animation.

What's happening? Is there a way I can get UILabels to animate their size?


This is the code I use to modify and animate the layout:

self.constraintWhiteHeight.constant = secondaryHeight;

[UIView animateWithDuration:5.0 animations:^{
    [self layoutIfNeeded];
}];

This is the code I use to create constraints:

// ------ Top -----
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self
                                                              attribute:NSLayoutAttributeTop
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.labelWhite
                                                              attribute:NSLayoutAttributeTop
                                                             multiplier:1.0
                                                               constant:0];
[self addConstraint:constraint];

// ------ Left -----
self.constraintWhiteLeft = [NSLayoutConstraint constraintWithItem:self
                                                        attribute:NSLayoutAttributeLeft
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:self.labelWhite
                                                        attribute:NSLayoutAttributeLeft
                                                       multiplier:1.0
                                                         constant:0];

// ------ Width -----
NSString *constraintString = [NSString stringWithFormat:@"H:[_labelWhite(==%.0f)]", self.bounds.size.width];
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraintString
                                                               options:0
                                                               metrics:nil
                                                                 views:NSDictionaryOfVariableBindings(_labelWhite)];
NSLog(@"constraints: %@", constraints);
self.constraintWhiteWidth = [constraints objectAtIndex:0];
[self.labelWhite addConstraints:constraints];

// ------ Height -----
constraintString = [NSString stringWithFormat:@"V:[_labelWhite(==%.0f)]", primaryHeight];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraintString
                                                      options:0
                                                      metrics:nil
                                                        views:NSDictionaryOfVariableBindings(_labelWhite)];
self.constraintWhiteHeight = [constraints objectAtIndex:0];
[self.labelWhite addConstraints:constraints];
+4
source share
1 answer

, - , , , . , , , - , . , , - CADisplayLink .

-(void)shrinkLabel {
    self.constraintWhiteHeight.constant -= 1;
    if (self.constraintWhiteHeight.constant >= secondaryHeight)
    [self performSelector:@selector(shrinkLabel) withObject:nil afterDelay:.05];
}

:

, , , . - UIView ( , ) UILabel , X centerY . , , WithDuration: :

enter image description here

-(void)shrinkLabel {
    self.yellowViewHeightCon.constant = secondaryHeight;
    [UIView animateWithDuration:5 animations:^{
        [self layoutIfNeeded];
    }];
}
+2

Source: https://habr.com/ru/post/1541214/


All Articles