我怎么动画约束的变化? [英] How do I animate constraint changes?

查看:179
本文介绍了我怎么动画约束的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更新了旧的应用程序与 AdBannerView 键,当没有广告,它的幻灯片关闭屏幕。当有广告它的幻灯片在屏幕上。基本的东西。

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 screen. Basic stuff.

旧的风格,我在一个动画块设置帧。
新的风格,我有一个 IBOutlet中来决定的Y位置,在这种情况下,从上海华底部它的距离,并修改常量的约束。

Old style, I set the frame in an animation block. New style, I have a IBOutlet to the constraint which determines the Y position, in this case it's distance from the bottom of the superview, and modify the constant.

- (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.

更新:我重新看了WWDC12视频<一个href=\"https://developer.apple.com/devcenter/download.action?path=/videos/wwdc_2012__sd/session_228__best_practices_for_mastering_auto_layout.mov\">Best实践涵盖动画掌握自动布局。它讨论了如何使用 CoreAnimation 更新约束。

UPDATE: I re-watched WWDC12 video "Best Practices for Mastering Auto Layout" which covers animation. It discusses how to update constraints using CoreAnimation.


我试着用下面的code,但得到同样的结果。

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.

推荐答案

两个重要注意事项:


  1. 您需要调用 layoutIfNeeded 动画块中。 <一href=\"https://developer.apple.com/library/watchos/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html#//apple_ref/doc/uid/TP40010853-CH5-SW15\">Apple其实建议您在动画块之前调用它一次,以确保所有悬而未决的布局操作已经完成

您需要专门调用它具有在父视图(如 self.view ),而不是子视图约束连接到它。这样做将更新的所有的约束的意见,包括动画可能被限制为你改变(例如视图B的约束视图连接要查看的底部,你只改变了其他视图查看A的顶部偏移和你想查看B到它动画)

You need to call it specifically on the parent view (e.g. self.view), not the child view that has the constraints attached to it. Doing so will update all constrained views, including animating other views that might be constrained to the view that you changed the constraint of (e.g. View B is attached to the bottom of View A and you just changed View A's top offset and you want View B to animate with it)

试试这个:

- (void)moveBannerOffScreen {
    [self.view layoutIfNeeded];

    _addBannerDistanceFromBottomConstraint.constant = -32;
    [UIView animateWithDuration:5
        animations:^{
            [self.view layoutIfNeeded]; // Called on parent view
        }];
    bannerIsVisible = FALSE;
}

- (void)moveBannerOnScreen { 
    [self.view layoutIfNeeded];

    _addBannerDistanceFromBottomConstraint.constant = 0;
    [UIView animateWithDuration:5
        animations:^{
            [self.view layoutIfNeeded]; // Called on parent view
        }];
    bannerIsVisible = TRUE;
}

以及斯威夫特办法....

_addBannerDistanceFromBottomConstraint.constant = 0;

UIView.animateWithDuration(5) {
    self.view.layoutIfNeeded()
}

这篇关于我怎么动画约束的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆