如何为约束更改设置动画?

坚强是说给别人听的谎言 提交于 2020-08-10 19:31:35

问题:

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 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/如何为约束更改设置动画
参考二: https://oldbug.net/q/qxfU/How-do-I-animate-constraint-changes
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!