ios - UIPresentationController preferredContentSize 更新动画

标签 ios animation uikit uipresentationcontroller

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

我想要实现的是能够动态更新此 View Controller View 的大小,只需更改它的 preferredContentSize。

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

-(void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container

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

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

立即调用 containerViewWillLayoutSubviews 并在没有动画的情况下更改框架。

-(void)containerViewWillLayoutSubviews
{
    self.presentedView.frame = [self frameOfPresentedViewInContainerView];
}

请帮我想办法,用动画调整它的大小。它必须是可能的,因为它会随着动画调整大小,例如在发生旋转时。

最佳答案

无需在 preferredContentSizeDidChangeForChildContentContainer 中设置框架,您只需在容器 View 上调用 setNeedsLayoutlayoutIfNeeded。 这会导致调用 containerViewWillLayoutSubviews,这将使用您的动画配置更新框架。

objective-C :

- (void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container
{
    [UIView animateWithDuration:1.0 animations:^{
        [self.containerView setNeedsLayout];
        [self.containerView layoutIfNeeded];
    } completion:nil]; 
}

swift :

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

替代方法

或者,您不能在 preferredContentSizeDidChange... 中设置动画 block ,而是将 preferredContentSize 的赋值放在动画 block 中。

这样您就可以更好地控制各个过渡的动画速度。

objective-C :

- (void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container
{
    [self.containerView setNeedsLayout];
    [self.containerView layoutIfNeeded];
}

// In view controller:

[UIView animateWithDuration:0.25 animations:^{
    self.preferredContentSize = CGSizeMake(self.view.bounds.width, 500.f);
}];

swift :

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)
}

关于ios - UIPresentationController preferredContentSize 更新动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33961039/

相关文章:

iphone - iPhone 和 iPad 中的 bundle 标识符和应用程序名称

iphone - 如何使用正则表达式在语句周围加上引号

javascript - 在鼠标悬停时运行 textillate.js 动画

javascript - HTML5 Canvas 动画不显示背景图像

swift - 如何处理 podfile 库的 "header file not found"? ('XYZ.h' 文件未找到)- 在 Xcode 中

php - 是否将 PHP 文件存储在项目目录中?

animation - 使用 Dart 为 SVG 元素制作动画

iOS:使用模态 pageFlip 在 UITableViewController 中制作奇怪的单元格动画

objective-c - 增加 UISearchDisplayController PopOver 结果的宽度

ios - 如何在 CarPlay 上播放视频?