首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIPresentationController preferredContentSize更新动画

UIPresentationController preferredContentSize更新动画
EN

Stack Overflow用户
提问于 2015-11-28 00:00:21
回答 2查看 3.1K关注 0票数 5

所以我有一个非常基本的UIPresentationController,基本上是以屏幕为中心显示contentView,它的大小是由视图控制器preferredContentSize决定的。(它与表示为FormSheet的常规模式视图控制器非常相似)。

我想要实现的是,能够动态更新这个视图控制器的视图大小,只需改变它的preferredContentSize即可。

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

代码语言:javascript
复制
-(void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container

但是如何在这里使用动画来调整图幅的大小呢?如果我打电话给你:

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

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

代码语言:javascript
复制
-(void)containerViewWillLayoutSubviews
{
    self.presentedView.frame = [self frameOfPresentedViewInContainerView];
}

请帮我找个办法,用动画来调整大小。这必须是可能的,因为它可以随动画调整大小,例如当发生旋转时。

EN

回答 2

Stack Overflow用户

发布于 2016-05-30 18:19:33

你必须使用自动布局。使出口从你的容器高度约束:

代码语言:javascript
复制
@property (nonatomic, strong, readwrite) IBOutlet NSLayoutConstraint *drawerViewHeightConstraint;

然后添加以下代码:

代码语言:javascript
复制
- (void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container
{
    [super preferredContentSizeDidChangeForChildContentContainer:container];
    self.drawerViewHeightConstraint.constant = container.preferredContentSize.height;

    [UIView animateWithDuration:0.25
                     animations:^{
                       [self.view layoutIfNeeded];
                     }];
}
票数 1
EN

Stack Overflow用户

发布于 2020-07-04 01:55:06

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

Objective-C:

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

Swift:

代码语言:javascript
复制
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...中不使用动画块,而是将preferredContentSize的指定放置在动画块中。

这样,您可以更好地控制各个变换的动画速度。

Objective-C:

代码语言:javascript
复制
- (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:

代码语言:javascript
复制
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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33961039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档