gpt4 book ai didi

ios - 自定义过渡动画在关闭时不调用 VC 生命周期方法

转载 作者:IT王子 更新时间:2023-10-29 07:42:39 25 4
gpt4 key购买 nike

所以我构建了一个自定义呈现过渡动画,一切似乎都运行良好,除了 View Controller 生命周期方法没有在关闭时被调用。

在呈现 Controller 之前,我使用 UIModalPresentationCustom 样式将呈现的 VC 保留在 View 层次结构中,但是一旦我关闭呈现的 VC,我的呈现 Controller 就不会调用 viewWillAppear 和 viewDidAppear。我是否缺少需要显式调用才能触发这些方法的步骤?我知道手动调用这些方法不是正确的解决方案。

这是我的解散动画代码。我基本上是在为表单覆盖 View 设置动画,以在关闭时缩小到 Collection View 单元格的大小。

- (void)_animateDismissingTransitionWithContext:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

UICollectionView *destinationCollectionView = toCollectionViewController.collectionView;
UICollectionViewCell *destinationCollectionViewCell = [self _destinationCellFromContext:transitionContext];
UIView *containerView = transitionContext.containerView;

// Calculate frames
CGRect startFrame = fromEventDetailViewController.detailContainerView.frame;
CGRect endFrame = [destinationCollectionView convertRect:destinationCollectionViewCell.frame toView:containerView];

// Add overlay
UIView *overlayView = [UIView new];
overlayView.backgroundColor = [UIColor overlayBackground];
overlayView.frame = containerView.bounds;
overlayView.alpha = 1.0f;
[containerView addSubview:overlayView];

// Add fake detail container view
UIView *fakeContainerView = [UIView new];
fakeContainerView.backgroundColor = fromEventDetailViewController.detailContainerView.backgroundColor;
fakeContainerView.frame = startFrame;
[containerView addSubview:fakeContainerView];

// Hide from view controller
fromEventDetailViewController.view.alpha = 0.0f;

[UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:0.2f options:UIViewAnimationOptionCurveEaseOut animations:^{
fakeContainerView.frame = endFrame;
fakeContainerView.backgroundColor = [UIColor eventCellBackground];

overlayView.alpha = 0.0f;
} completion:^(BOOL finished) {
[fromEventDetailViewController.view removeFromSuperview];
[overlayView removeFromSuperview];
[fakeContainerView removeFromSuperview];

[transitionContext completeTransition:YES];
}];
}

最佳答案

另一种解决方案可能是使用beginAppearanceTransition:endAppearanceTransition:。根据文档:

If you are implementing a custom container controller, use this method to tell the child that its views are about to appear or disappear. Do not invoke viewWillAppear:, viewWillDisappear:, viewDidAppear:, or viewDidDisappear: directly.

以下是我如何使用它们:

- (void)animationEnded:(BOOL)transitionCompleted
{
if (!transitionCompleted)
{
_toViewController.view.transform = CGAffineTransformIdentity;
}
else
{
[_toViewController endAppearanceTransition];
}
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

[toViewController beginAppearanceTransition:YES animated:YES];
// ... other code
}

但我仍然认为自定义模态展示不这样做很奇怪。

关于ios - 自定义过渡动画在关闭时不调用 VC 生命周期方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25488267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com