所以我有一个非常基本的 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 上调用 setNeedsLayout
和 layoutIfNeeded
。
这会导致调用 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/