• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIPresentationController preferredContentSize 更新动画

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:23:15 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

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

-(void)preferredContentSizeDidChangeForChildContentContainerid<UIContentContainer>)container

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

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

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

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

请帮我找到一种方法,用动画调整它的大小。这一定是可能的,因为它会通过动画调整大小,例如在发生旋转时。



Best Answer-推荐答案


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

objective-C :

- (void)preferredContentSizeDidChangeForChildContentContainerid<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)preferredContentSizeDidChangeForChildContentContainerid<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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap