如何动画约束更改? - How do I animate constraint changes?

问题:

I'm updating an old app with an AdBannerView and when there is no ad, it slides off screen.我正在使用AdBannerView更新旧应用程序,当没有广告时,它会滑出屏幕。 When there is an ad it slides on the screen.当有广告时,它会在屏幕上滑动。 Basic stuff.基本的东西。

Old style, I set the frame in an animation block.旧风格,我在动画块中设置了框架。 New style, I have a IBOutlet to the auto-layout constraint which determines the Y position, in this case it's distance from the bottom of the superview, and modify the constant:新样式,我有一个IBOutlet到自动布局约束,它确定Y位置,在这种情况下,它是与超级视图底部的距离,并修改常量:

- (void)moveBannerOffScreen {
    [UIView animateWithDuration:5 animations:^{
        _addBannerDistanceFromBottomConstraint.constant = -32;
    }];
    bannerIsVisible = FALSE;
}

- (void)moveBannerOnScreen {
    [UIView animateWithDuration:5 animations:^{
        _addBannerDistanceFromBottomConstraint.constant = 0;
    }];
    bannerIsVisible = TRUE;
}

And the banner moves, exactly as expected, but no animation.横幅移动,完全符合预期,但没有动画。


UPDATE: I re-watched WWDC 12 talk Best Practices for Mastering Auto Layout which covers animation.更新:我重新观看了涵盖动画的WWDC 12 演讲“掌握自动布局的最佳实践” It discusses how to update constraints using CoreAnimation :它讨论了如何使用CoreAnimation更新约束:

I've tried with the following code, but get the exact same results:我尝试使用以下代码,但得到了完全相同的结果:

- (void)moveBannerOffScreen {
    _addBannerDistanceFromBottomConstraint.constant = -32;
    [UIView animateWithDuration:2 animations:^{
        [self.view setNeedsLayout];
    }];
    bannerIsVisible = FALSE;
}

- (void)moveBannerOnScreen {
    _addBannerDistanceFromBottomConstraint.constant = 0;
    [UIView animateWithDuration:2 animations:^{
        [self.view setNeedsLayout];
    }];
    bannerIsVisible = TRUE;
}

On a side note, I have checked numerous times and this is being executed on the main thread.顺便说一句,我已经检查了很多次,这是在线程上执行的。


解决方案:

参考一: https://stackoom.com/question/qxfU
参考二: How do I animate constraint changes?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章