UIVisualEffectView在呈现新视图时创建不需要的阴影

时间:2016-01-01 08:44:52

标签: swift uiviewcontroller shadow blur

在我的自定义演示文稿转换中,我创建了一个新的视图控制器,它将预先显示在当前活动视图控制器之上(参见屏幕截图)。不知怎的,蓝色视图控制器后面有一个阴影,我不知道它来自哪里。有没有办法阻止那个阴影?

项目完全为空,只有2个空视图控制器。

这是我使用的代码:

class ViewController: UIViewController {

    let transitionDelegate = TransitionManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .yellowColor()

        let button = UIButton(type: .System)
        button.frame = CGRectMake(10, 10, 50, 50)
        button.addTarget(self, action: "test:", forControlEvents: .TouchUpInside)
        button.backgroundColor = UIColor.redColor()

        view.addSubview(button)
    }

    func test(sender: UIButton) {
        let destination = UIViewController()
        destination.view.backgroundColor = .blueColor()

        destination.transitioningDelegate = transitionDelegate
        destination.modalPresentationStyle = .Custom
        presentViewController(destination, animated: true, completion: nil)
    }
}

展示视图的代码:

class PresentingTransition: NSObject, UIViewControllerAnimatedTransitioning {

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

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let presented = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        let container = transitionContext.containerView()!
        let durations = transitionDuration(transitionContext)

        presented.view.alpha = 0

        container.addSubview(presented.view)

        UIView.animateWithDuration(durations, animations: { presented.view.alpha = 1 }) { transitionContext.completeTransition($0) }
    }
}

处理呈现视图控制器的代码:

class PresentationController: UIPresentationController {

    var background: UIView!

    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)

        prepareBackground()
    }

    func prepareBackground() {
        self.background = UIView(frame: presentingViewController.view.bounds)

        let blur = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
        blur.frame = background.bounds
        blur.autoresizingMask = [.FlexibleHeight, .FlexibleWidth]

        background.addSubview(blur)

        let tapRecognizer = UITapGestureRecognizer(target: self, action: "backgroundTapped:")
        background.addGestureRecognizer(tapRecognizer)
    }

    func backgroundTapped(tapRecognizer: UITapGestureRecognizer) {
        presentingViewController.dismissViewControllerAnimated(true, completion: nil)
    }

    override func presentationTransitionWillBegin() {
        let container = containerView!

        background.frame = container.bounds
        background.alpha = 0.0

        container.insertSubview(background, atIndex: 0)

        presentedViewController.transitionCoordinator()?.animateAlongsideTransition({ _ in self.background.alpha = 1.0 }, completion: nil)
    }

    override func dismissalTransitionWillBegin() {
        presentedViewController.transitionCoordinator()?.animateAlongsideTransition({ _ in self.background.alpha = 0.0 }, completion: nil)
    }

    override func frameOfPresentedViewInContainerView() -> CGRect {
        return containerView!.bounds.insetBy(dx: 100, dy: 100)
    }

    override func containerViewWillLayoutSubviews() {
        background.frame = containerView!.bounds
        presentedView()!.frame = frameOfPresentedViewInContainerView()
    }
}

View presented

0 个答案:

没有答案