简体   繁体   English

使用自定义 UIViewController 动画制作动画后呈现的视图控制器消失

[英]Presented view controller disappears after animation using custom UIViewController animations

I've looked around for an answer for this and spent the last two hours pulling my hair out to no end.我已经环顾四周寻找答案,并在过去的两个小时里把我的头发拉到了尽头。

I'm implementing a very basic custom view controller transition animation, which simply zooms in on the presenting view controller and grows in the presented view controller.我正在实现一个非常基本的自定义视图控制器过渡动画,它只是放大呈现的视图控制器并在呈现的视图控制器中增长。 It adds a fade effect (0 to 1 alpha and visa versa).它添加了淡入淡出效果(0 到 1 alpha,反之亦然)。

It works fine when presenting the view controller, however when dismissing, it brings the presenting view controller back in all the way to fill the screen, but then it inexplicably disappears.它在呈现视图控制器时工作正常,但是在关闭时,它将presenting视图控制器一直带回以填充屏幕,但随后它莫名其妙地消失了。 I'm not doing anything after these animations to alter the alpha or the hidden values, it's pretty much a fresh project.在这些动画之后我没有做任何改变 alpha 或隐藏值的事情,这几乎是一个全新的项目。 I've been developing iOS applications for 3 years so I suspect this may be a bug, unless someone can find out where I'm going wrong.我已经开发 iOS 应用程序 3 年了,所以我怀疑这可能是一个错误,除非有人能找出我哪里出错了。

class FadeAndGrowAnimationController : NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController!, presentingController presenting: UIViewController!, sourceController source: UIViewController!) -> UIViewControllerAnimatedTransitioning! {
    return self
}

func animationControllerForDismissedController(dismissed: UIViewController!) -> UIViewControllerAnimatedTransitioning! {
    return self
}

func transitionDuration(transitionContext: UIViewControllerContextTransitioning!) -> NSTimeInterval {
    return 2
}

func animateTransition(transitionContext: UIViewControllerContextTransitioning!) {
    let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as UIViewController
    let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) as UIViewController

    toViewController.view.transform = CGAffineTransformMakeScale(0.5, 0.5)
    toViewController.view.alpha = 0

    transitionContext.containerView().addSubview(fromViewController.view)
    transitionContext.containerView().addSubview(toViewController.view)
    transitionContext.containerView().bringSubviewToFront(toViewController.view)

    UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: {
        fromViewController.view.transform = CGAffineTransformScale(fromViewController.view.transform, 2, 2)
        fromViewController.view.alpha = 1

        toViewController.view.transform = CGAffineTransformMakeScale(1, 1)
        toViewController.view.alpha = 1
    }, completion: { finished in
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
    })
}

} }

And the code to present:以及要呈现的代码:

    let targetViewController = self.storyboard.instantiateViewControllerWithIdentifier("Level1ViewController") as Level1ViewController
    let td = FadeAndGrowAnimationController()

    targetViewController.transitioningDelegate = td
    targetViewController.modalPresentationStyle = .Custom

    self.presentViewController(targetViewController, animated: true, completion: nil)

As you can see, a fairly basic animation.如您所见,这是一个相当基本的动画。 Am I missing something here?我在这里错过了什么吗? Like I said, it presents perfectly fine, then dismisses 99.99% perfectly fine, yet the view controller underneath after the dismissal is inexplicably removed.就像我说的,它表现得非常好,然后完美地消除了 99.99%,但是在消除后下面的视图控制器被莫名其妙地移除了。 The iPad shows a blank screen - totally black - after this happens.发生这种情况后,iPad 会显示一个空白屏幕 - 全黑。

This seems to be an iOS8 bug.这似乎是一个 iOS8 错误。 I found a solution but it is ghetto.我找到了一个解决方案,但它是贫民窟。 After the transition when a view should be on-screen but isn't, it needs to be added back to the window like this:在视图应该在屏幕上但不在屏幕上的转换之后,需要像这样将其添加回窗口:

BOOL canceled = [transitionContext transitionWasCancelled];
[transitionContext completeTransition:!canceled];
if (!canceled)
    {
    [[UIApplication sharedApplication].keyWindow addSubview: toViewController.view];
    }

You might need to play around with which view you add back to the window, whether to do it in canceled or !canceled, and perhaps making sure to only do it on dismissal and not presentation.您可能需要考虑添加回窗口的视图,无论是在取消还是取消时执行,也许确保仅在取消时执行而不是在演示时执行。

Sources: Container view disappearing on completeTransition: http://joystate.wordpress.com/2014/09/02/ios8-and-custom-uiviewcontrollers-transitions/来源: 在 completeTransition 上消失的容器视图: http : //joystate.wordpress.com/2014/09/02/ios8-and-custom-uiviewcontrollers-transitions/

had the same problem ios 8.1有同样的问题 ios 8.1

my swift code after completeTransition completeTransition 后我的快速代码

if let window = UIApplication.sharedApplication().keyWindow {
    if let viewController = window.rootViewController {
        window.addSubview(viewController.view)
    }
}

I was having the same problem when dismissing a content view controller.关闭内容视图控制器时我遇到了同样的问题。 My app has this parent view controller showing a child view controller.我的应用程序有这个显示子视图控制器的父视图控制器。 then when a subview in the child is tapped, it shows another vc (which I am calling the content vc)然后当子视图中的子视图被点击时,它会显示另一个 vc(我称之为内容 vc)

My problem is that, when dismissing the contentVC, it should go to child VC but as soon as my custom transition finishes, childVC suddenly disappears, showing the parent VC instead.我的问题是,当解除 contentVC 时,它应该转到子 VC,但是一旦我的自定义转换完成,childVC 突然消失,而是显示父 VC。

What I did to solve this issue is to我为解决这个问题所做的是

  1. change the .modalPresentationStyle of the childVC presented by parentVC from the default .automatic to .fullscreen .改变.modalPresentationStyle的childVC呈现由parentVC从默认.automatic.fullscreen
  2. Then changed the .modalPresentationStyle of contentVC to .fullscreen as well.然后将 contentVC 的.fullscreen也更改为.modalPresentationStyle

This solves the issue.这解决了这个问题。 but it won't show your child VC as a card sheet (when using .overCurrentContext or automatic) which is new in iOS 13.但它不会将您的孩子 VC 显示为 iOS 13 中新增的卡片表(使用.overCurrentContext或自动时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 呈现动画后呈现的UIViewController消失 - Presented UIViewController disappears after segue animation 呈现视图 controller 在 animation 之后消失 - Presenting View controller disappears after animation UIViewController 交互式过渡 - 取消交互式关闭时显示的视图消失 - UIViewController interactive transition - Presented view disappears when interactive dismiss is canceled 使用与呈现的动画不同的动画关闭模态视图控制器 - Dismissing a modal view controller using a different animation than it was presented with 在通过委托解除其呈现的模态视图控制器后,刷新UIViewController中的数据 - Refreshing data in a UIViewController after dismissing its presented modal view controller via delegate 出现模态视图 controller 时暂停 animation - Pause animation when modal view controller is presented UIViewController-将没有Flash的呈现视图控制器切换为呈现一个? - UIViewController - switch presented view controller without flash to presenting one? UIViewController的视图在显示后立即更改尺寸 - UIViewController's view changes dimension immediately after being presented 出现2/3次后,不再显示模态视图控制器 - Modal view controller not presented anymore after being presented 2/3 times 旋转后关闭呈现的视图 controller - Dismiss the presented view controller after rotataion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM