呈现具有白色和透明背景的模态UIViewController时的黑色工件

时间:2017-08-16 07:29:53

标签: ios swift uikit

我使用UIModalPresentationCustom呈现模态UIViewController,所以我也使用了UIPresentationController。我所呈现的我的ViewController为背景设置了清晰的颜色(在故事板图片中我设置了蓝色以显示哪个视图是透明的),但也有一个白色的UIView。

storyboard picture here

当我点击白色半视图外的关闭按钮时,有些奇怪。当我点击该按钮时,会显示一些黑色瑕疵,并且仅当关闭按钮具有类似于图片的位置并且存在白色视图时才会出现。如果我向上移动按钮并且它完全在白色视图上,则不会出现黑色瑕疵。此外,当我为此白色视图设置清晰的颜色时,所以我的所有viewcontroller都将具有透明背景 - 不会有任何伪影。

也许以前有人遇到过这个问题?

1 个答案:

答案 0 :(得分:2)

@ moni15,我之前尝试调试视图层次结构但是没有可见的黑色工件,当我打开视图帧时,一切看起来都很好。这看起来像一些渲染问题,或者iOS错误?

这是Presentation和Dimiss过渡的代码:

    class TransitionPresentationAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
        let containerView = transitionContext.containerView

        let animationDuration = self .transitionDuration(using: transitionContext)

        toViewController.view.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
        toViewController.view.layer.shadowColor = UIColor.black.cgColor
        toViewController.view.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
        toViewController.view.layer.shadowOpacity = 0.3
        toViewController.view.layer.cornerRadius = 4.0
        toViewController.view.clipsToBounds = true

        containerView.addSubview(toViewController.view)

        UIView.animate(withDuration: animationDuration, animations: { () -> Void in
            toViewController.view.transform = CGAffineTransform.identity
        }, completion: { (finished) -> Void in
            transitionContext.completeTransition(finished)
        })
    }
}

class TransitionDismissAnimator : NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
        let animationDuration = self .transitionDuration(using: transitionContext)

        UIView.animate(withDuration: animationDuration, animations: { () -> Void in
            fromViewController.view.alpha = 0.0
            fromViewController.view.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
        }) { (finished) -> Void in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        }
    }
}

解决了!问题出现在TransitionPresentationAnimator类中,而CGAffineTransform出错了。现在我不再使用它了。如果有人遇到类似的问题,我会粘贴我的代码:

class TransitionPresentationAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        to = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
            from = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
            let container = transitionContext.containerView
            container.addSubview(to.view)

            to.view.bounds.origin = CGPoint(x: 0, y: -from.view.bounds.size.height)
            UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [.curveEaseOut], animations: {
                self.to.view.bounds = self.from.view.bounds
            }) { (completed) in
                transitionContext.completeTransition(completed)
            }
    }
}