如何動畫約束更改? - 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?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章