菜鸟教程小白 发表于 2022-12-12 17:23:15

ios - UIPresentationController preferredContentSize 更新动画


                                            <p><p>所以我有一个非常基本的 UIPresentationController,它基本上以屏幕为中心显示 contentView,它的大小由 ViewControllerPreferredContentSize 决定。 (它非常类似于以 FormSheet 形式呈现的常规模态视图 Controller )。 </p>

<p>我想要实现的是能够动态更新此 ViewControllerView 的大小,只需更改它的preferredContentSize。</p>

<p>当我设置preferredContentSize 时,我的UIPresentationController 子类会在以下位置接收有关它的信息:</p>

<pre><code>-(void)preferredContentSizeDidChangeForChildContentContainer:(id&lt;UIContentContainer&gt;)container
</code></pre>

<p>但是我怎样才能从这里用动画调整 View 框架的大小?如果我只是打电话:</p>

<pre><code>-(void)preferredContentSizeDidChangeForChildContentContainer:(id&lt;UIContentContainer&gt;)container
{
    [UIView animateWithDuration:1.0 animations:^{
      self.presentedView.frame = ;
    } completion:nil];
}
</code></pre>

<p>立即被调用 containerViewWillLayoutSubviews 并且框架在没有动画的情况下被改变。</p>

<pre><code>-(void)containerViewWillLayoutSubviews
{
    self.presentedView.frame = ;
}
</code></pre>

<p>请帮我找到一种方法,用动画调整它的大小。这一定是可能的,因为它会通过动画调整大小,例如在发生旋转时。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您只需在容器 View 上调用 <code>setNeedsLayout</code> 和 <code>layoutIfNeeded</code>,而不是从 <code>preferredContentSizeDidChangeForChildContentContainer</code> 中设置框架。
这会导致调用 <code>containerViewWillLayoutSubviews</code>,这将使用您的动画配置更新帧。</p>
<p><strong> objective-C :</strong></p>
<pre><code>- (void)preferredContentSizeDidChangeForChildContentContainer:(id&lt;UIContentContainer&gt;)container
{
    [UIView animateWithDuration:1.0 animations:^{
      ;
      ;
    } completion:nil];
}
</code></pre>
<p><strong> swift :</strong></p>
<pre><code>override func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer) {
    super.preferredContentSizeDidChange(forChildContentContainer: container)
   
    guard let containerView = containerView else {
      return
    }
   
    UIView.animate(withDuration: 1.0, animations: {
      containerView.setNeedsLayout()
      containerView.layoutIfNeeded()
    })
}
</code></pre>
<h2>替代方法</h2>
<p>或者,您不能在 <code>preferredContentSizeDidChange...</code> 中使用动画 block ,而是将 <code>preferredContentSize</code> 的分配放在动画 block 中。</p>
<p>通过这种方式,您可以更好地控制各个过渡的动画速度。</p>
<p><strong> objective-C :</strong></p>
<pre><code>- (void)preferredContentSizeDidChangeForChildContentContainer:(id&lt;UIContentContainer&gt;)container
{
    ;
    ;
}

// In view controller:

[UIView animateWithDuration:0.25 animations:^{
    self.preferredContentSize = CGSizeMake(self.view.bounds.width, 500.f);
}];
</code></pre>
<p><strong> swift :</strong></p>
<pre><code>override func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer) {
    super.preferredContentSizeDidChange(forChildContentContainer: container)
   
    guard let containerView = containerView else {
      return
    }
   
    containerView.setNeedsLayout()
    containerView.layoutIfNeeded()
}

// In view controller

UIView.animate(withDuration: 0.25) {
    self.preferredContentSize = CGSize(width: self.view.width, height: 500)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIPresentationController preferredContentSize 更新动画,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33961039/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33961039/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIPresentationController preferredContentSize 更新动画