我正在使用 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 the screen. Basic stuff.
旧样式,我将帧设置在动画块中.新样式,我有一个 IBOutlet
到自动布局约束,它确定 Y
位置,在这种情况下它是与超级视图底部的距离,并修改常量:
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:
- (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.
更新:我重新观看了 WWDC 12 talk Best掌握自动布局的实践,涵盖动画.它讨论了如何使用 CoreAnimation 更新约束:
UPDATE: I re-watched WWDC 12 talk Best Practices for Mastering Auto Layout which covers animation. It discusses how to update constraints using 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;
}
顺便说一句,我已经检查了很多次,这是在 main 线程上执行的.
On a side note, I have checked numerous times and this is being executed on the main thread.
两个重要说明:
你需要在动画块中调用layoutIfNeeded
.苹果实际上建议你在动画块之前调用一次,以确保所有待处理的布局操作都已完成
You need to call layoutIfNeeded
within the animation block. Apple actually recommends you call it once before the animation block to ensure that all pending layout operations have been completed
您需要在 父视图(例如 self.view
)上专门调用它,而不是附加了约束的子视图.这样做会更新所有个受约束的视图,包括为可能被约束到您更改约束的视图的其他视图设置动画(例如,视图 B 附加到视图 A 的底部,而您刚刚更改了视图 A顶部偏移,并且您希望 View 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)
试试这个:
Objective-C
- (void)moveBannerOffScreen {
[self.view layoutIfNeeded];
[UIView animateWithDuration:5
animations:^{
self._addBannerDistanceFromBottomConstraint.constant = -32;
[self.view layoutIfNeeded]; // Called on parent view
}];
bannerIsVisible = FALSE;
}
- (void)moveBannerOnScreen {
[self.view layoutIfNeeded];
[UIView animateWithDuration:5
animations:^{
self._addBannerDistanceFromBottomConstraint.constant = 0;
[self.view layoutIfNeeded]; // Called on parent view
}];
bannerIsVisible = TRUE;
}
斯威夫特 3
UIView.animate(withDuration: 5) {
self._addBannerDistanceFromBottomConstraint.constant = 0
self.view.layoutIfNeeded()
}
这篇关于如何为约束更改设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!