How do I animate constraint changes?

后端 未结 12 1813
野的像风
野的像风 2020-11-21 23:22

I\'m updating an old app with an AdBannerView and when there is no ad, it slides off screen. When there is an ad it slides on the screen. Basic stuff.

O

12条回答
  •  萌比男神i
    2020-11-22 00:03

    There is an article talk about this: http://weblog.invasivecode.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was

    In which, he coded like this:

    - (void)handleTapFrom:(UIGestureRecognizer *)gesture {
        if (_isVisible) {
            _isVisible = NO;
            self.topConstraint.constant = -44.;    // 1
            [self.navbar setNeedsUpdateConstraints];  // 2
            [UIView animateWithDuration:.3 animations:^{
                [self.navbar layoutIfNeeded]; // 3
            }];
        } else {
            _isVisible = YES;
            self.topConstraint.constant = 0.;
            [self.navbar setNeedsUpdateConstraints];
            [UIView animateWithDuration:.3 animations:^{
                [self.navbar layoutIfNeeded];
            }];
        }
    }
    

    Hope it helps.

提交回复
热议问题