• <i id='xXKQ7'><tr id='xXKQ7'><dt id='xXKQ7'><q id='xXKQ7'><span id='xXKQ7'><b id='xXKQ7'><form id='xXKQ7'><ins id='xXKQ7'></ins><ul id='xXKQ7'></ul><sub id='xXKQ7'></sub></form><legend id='xXKQ7'></legend><bdo id='xXKQ7'><pre id='xXKQ7'><center id='xXKQ7'></center></pre></bdo></b><th id='xXKQ7'></th></span></q></dt></tr></i><div id='xXKQ7'><tfoot id='xXKQ7'></tfoot><dl id='xXKQ7'><fieldset id='xXKQ7'></fieldset></dl></div>
  • <tfoot id='xXKQ7'></tfoot>
    <legend id='xXKQ7'><style id='xXKQ7'><dir id='xXKQ7'><q id='xXKQ7'></q></dir></style></legend>

      <small id='xXKQ7'></small><noframes id='xXKQ7'>

          <bdo id='xXKQ7'></bdo><ul id='xXKQ7'></ul>
      1. 如何为约束更改设置动画?

        How do I animate constraint changes?(如何为约束更改设置动画?)
      2. <legend id='fAEoF'><style id='fAEoF'><dir id='fAEoF'><q id='fAEoF'></q></dir></style></legend>

        • <small id='fAEoF'></small><noframes id='fAEoF'>

                <tfoot id='fAEoF'></tfoot>
                  <tbody id='fAEoF'></tbody>

                <i id='fAEoF'><tr id='fAEoF'><dt id='fAEoF'><q id='fAEoF'><span id='fAEoF'><b id='fAEoF'><form id='fAEoF'><ins id='fAEoF'></ins><ul id='fAEoF'></ul><sub id='fAEoF'></sub></form><legend id='fAEoF'></legend><bdo id='fAEoF'><pre id='fAEoF'><center id='fAEoF'></center></pre></bdo></b><th id='fAEoF'></th></span></q></dt></tr></i><div id='fAEoF'><tfoot id='fAEoF'></tfoot><dl id='fAEoF'><fieldset id='fAEoF'></fieldset></dl></div>
                • <bdo id='fAEoF'></bdo><ul id='fAEoF'></ul>
                  本文介绍了如何为约束更改设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 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.

                  推荐答案

                  两个重要说明:

                  1. 你需要在动画块中调用layoutIfNeeded.苹果实际上建议你在动画块之前调用一次,以确保所有待处理的布局操作都已完成

                  1. 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()
                  }
                  

                  这篇关于如何为约束更改设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS AutoLayout - get frame size width(iOS AutoLayout - 获取帧大小宽度)
                  Auto layout constraints issue on iOS7 in UITableViewCell(UITableViewCell中iOS7上的自动布局约束问题)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5(自动布局中的弹簧:在 Xcode 5 中使用约束均匀分布视图)
                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员

                      <i id='xphfW'><tr id='xphfW'><dt id='xphfW'><q id='xphfW'><span id='xphfW'><b id='xphfW'><form id='xphfW'><ins id='xphfW'></ins><ul id='xphfW'></ul><sub id='xphfW'></sub></form><legend id='xphfW'></legend><bdo id='xphfW'><pre id='xphfW'><center id='xphfW'></center></pre></bdo></b><th id='xphfW'></th></span></q></dt></tr></i><div id='xphfW'><tfoot id='xphfW'></tfoot><dl id='xphfW'><fieldset id='xphfW'></fieldset></dl></div>

                    • <small id='xphfW'></small><noframes id='xphfW'>

                      <legend id='xphfW'><style id='xphfW'><dir id='xphfW'><q id='xphfW'></q></dir></style></legend>
                        <bdo id='xphfW'></bdo><ul id='xphfW'></ul>
                        <tfoot id='xphfW'></tfoot>
                              <tbody id='xphfW'></tbody>