0

Как достичь такого эффекта модального окна? Когда активное view уходит на задний план а модальное окно выезжает не на всю высоту экрана

1
  • ну модальное окно точно не подойдет. попробуйте либо добавлять свой вью прямо к controller.view либо можно попробовать создать еще один UIWindow и все это делать на нем. 14 сен 2016 в 12:28

1 ответ 1

1

Возможно не совсем как в почте, немного переделал имеющийся вариант.

Здесь исходники примера и gif-демка на GitHub

Результат:

введите сюда описание изображения

Теперь по порядку:

1 Extension для контроллера откуда:

extension ViewController: UIViewControllerTransitioningDelegate {

    func animationController(
        forPresented presented: UIViewController,
        presenting: UIViewController,
        source: UIViewController) ->
        UIViewControllerAnimatedTransitioning? {
            transition.presenting = true
            transition.duration = 0.5
            return transition
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        transition.presenting = false
        return transition
    }
}

2 Переход на 2 контроллер:

let secondViewController = storyboard!.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
secondViewController.transitioningDelegate = self
secondViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
present(secondViewController, animated: true, completion: nil)

3 UIViewControllerTransitioningDelegate протокол для второго контроллера:

class SecondViewController: UIViewController, UIViewControllerTransitioningDelegate {

4 Класс с необходимой анимацией:

import UIKit

class TransitionMailLikeAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    var duration    = 0.6
    var presenting  = true

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

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
        let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
        let toView = toViewController.view
        let containerView = transitionContext.containerView
        if presenting {
            containerView.addSubview(toView!)
        }
        let bottomViewController = presenting ? fromViewController : toViewController
        let bottomPresentingView = bottomViewController.view
        let topViewController = presenting ? toViewController : fromViewController
        let topPresentedView = topViewController.view
        var topPresentedFrame = transitionContext.finalFrame(for: topViewController)
        if presenting {
            topPresentedFrame = CGRect(x: 0, y: 38, width: topPresentedFrame.width, height: topPresentedFrame.height - 38)
        }
        var topDismissedFrame = topPresentedFrame
        topDismissedFrame.origin.y += topDismissedFrame.size.height
        let topInitialFrame = presenting ? topDismissedFrame : topPresentedFrame
        let topFinalFrame = presenting ? topPresentedFrame : topDismissedFrame
        topPresentedView?.frame = topInitialFrame
        UIView.animate(withDuration: duration, animations: {() -> Void in
            topPresentedView?.frame = topFinalFrame
            let scalingFactor: CGFloat = self.presenting ? 0.91 : 1.0
            bottomPresentingView?.transform = CGAffineTransform.identity.scaledBy(x: scalingFactor, y: scalingFactor)
            }, completion: {(finished: Bool) -> Void in
                transitionContext.completeTransition(true)})
    }
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.