gpt4 book ai didi

ios - 以白色和透明背景模态呈现 UIViewController 时出现黑色伪影

转载 作者:行者123 更新时间:2023-11-28 12:18:14 25 4
gpt4 key购买 nike

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

storyboard picture here

当我点击处于半白色外部 View 中的关闭按钮时,出现了一些奇怪的情况。当我点击该按钮时,会显示一些黑色伪影,并且仅当关闭按钮的位置与图片相同并且有白色 View 时才会出现。如果我向上移动按钮并且它完全位于白色 View 中,则不会出现黑色伪影。此外,当我为这个白色 View 设置清晰的颜色时,我的所有 View Controller 都将具有透明背景 - 不会有伪影。

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

最佳答案

@moni15,我之前尝试调试 View 层次结构,但没有看到黑色伪影,而且当我打开 View 框架时,一切看起来都很好。这看起来像是一些渲染问题,或者可能是 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)
}
}
}

关于ios - 以白色和透明背景模态呈现 UIViewController 时出现黑色伪影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45707528/

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