如何激活约束更改?我正在用AdBannerView当没有广告的时候,它就会从屏幕上滑落。当有一个广告,它在屏幕上幻灯片。基本的东西。旧的风格,我把框架设置在一个动画块。新款式,我有一个IBOutlet对于确定Y位置的自动布局约束,在这种情况下,它与SuperView底部的距离,并修改常量:- (void)moveBannerOffScreen {
[UIView animateWithDuration:5 animations:^{
_addBannerDistanceFromBottomConstraint.constant = -32;
}];
bannerIsVisible = FALSE;}- (void)moveBannerOnScreen {
[UIView animateWithDuration:5 animations:^{
_addBannerDistanceFromBottomConstraint.constant = 0;
}];
bannerIsVisible = TRUE;}横幅的移动和预期完全一样,但是不动画。最新情况:我重新看了WWDC 12谈掌握汽车布局的最佳实践包括动画。讨论如何使用共动画: 我尝试了以下代码,但得到了完全相同的结果:- (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;}另外,我检查了很多次,这是在主线。
3 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
你需要打电话 layoutIfNeeded
在动画块中。实际上,Apple建议您在动画块之前调用它一次,以确保所有挂起的布局操作都已完成。 您需要在 父视图(如: self.view
),而不是附加约束的子视图。这样做将更新 全约束视图,包括动画其他可能被限制为您更改了视图A的约束的视图(例如,视图B附加到视图A的底部,而您刚刚更改了视图A的顶部偏移量,您希望视图B与它一起动画)
目标-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;}
SWIFT 3
UIView.animate(withDuration: 5) { self._addBannerDistanceFromBottomConstraint.constant = 0 self.view.layoutIfNeeded()}
慕哥9229398
TA贡献1877条经验 获得超6个赞
文档中的基本块动画
[containerView layoutIfNeeded]; // Ensures that all pending layout operations have been completed[UIView animateWithDuration:1.0 animations:^{ // Make all constraint changes here [containerView layoutIfNeeded]; // Forces the layout of the subtree animation block and then captures all of the frame changes}];
updateConstraints
调用子视图updateConstraint方法的动画块
[self.view layoutIfNeeded];[self.subView setNeedsUpdateConstraints];[self.subView updateConstraintsIfNeeded]; [UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionLayoutSubviews animations:^{ [self.view layoutIfNeeded];} completion:nil];
- (void)updateConstraints{ // Update some constraints [super updateConstraints];}
UISwitch
UITextField
- 3 回答
- 0 关注
- 608 浏览
添加回答
举报
0/150
提交
取消