Swift:取消UIPercentDrivenInteractiveTransition?

时间:2017-07-13 09:03:59

标签: ios swift uinavigationcontroller delegates

这是我的第一个iOS开发,因此我正在使用这个小项目来了解系统的工作原理以及语言(swift)的工作原理。

我正在尝试制作类似于Android应用程序和一定数量iOS应用程序的抽屉菜单。

我发现本教程很好地解释了如何操作以及它是如何工作的:here

现在因为我正在使用带有show的NavigationController,我必须修改它的完成方式。

我将 UIViewControllerTransitioningDelegate 交换到了 UINavigationControllerDelegate ,因此我可以覆盖 navigationController 函数。

这意味着我可以取出抽屉并将其解雇。它适用于按钮或手势。 我的问题如下:如果我没有完成将抽屉拖到足够远的距离以达到阈值并完成动画,它将被取消并隐藏。这一切都很好但是当发生这种情况时,没有调用dismiss函数意味着我在 PresentMenuAnimator 中放置的快照仍然在所有层的前面,我甚至被卡在那里虽然我可以与其背后的内容进行互动。

如何使用NavigationController捕获解雇或取消?这可能吗?

Interactor:

import UIKit


class Interactor:UIPercentDrivenInteractiveTransition {
    var hasStarted: Bool = false;
    var shouldFinish: Bool = false;

}

MenuHelper:

import Foundation
import UIKit

enum Direction {
    case Up
    case Down
    case Left
    case Right
}

struct MenuHelper {
    static let menuWith:CGFloat = 0.8;
    static let percentThreshold:CGFloat = 0.6;
    static let snapshotNumber = 12345;

    static func calculateProgress(translationInView:CGPoint, viewBounds:CGRect, direction: Direction) -> CGFloat {
        let pointOnAxis:CGFloat;
        let axisLength:CGFloat;

        switch direction {
        case .Up, .Down :
            pointOnAxis = translationInView.y;
            axisLength = viewBounds.height;
        case .Left, .Right :
            pointOnAxis = translationInView.x;
            axisLength = viewBounds.width;
        }
        let movementOnAxis = pointOnAxis/axisLength;
        let positiveMovementOnAxis:Float;
        let positiveMovementOnAxisPercent:Float;

        switch direction {
        case .Right, .Down:
            positiveMovementOnAxis = fmaxf(Float(movementOnAxis), 0.0);
            positiveMovementOnAxisPercent = fminf(positiveMovementOnAxis, 1.0);
            return CGFloat(positiveMovementOnAxisPercent);
        case .Left, .Up :
            positiveMovementOnAxis = fminf(Float(movementOnAxis), 0.0);
            positiveMovementOnAxisPercent = fmaxf(positiveMovementOnAxis, -1.0);
            return CGFloat(-positiveMovementOnAxisPercent);
        }
    }

    static func mapGestureStateToInteractor(gestureState:UIGestureRecognizerState, progress:CGFloat, interactor: Interactor?, triggerSegue: () -> Void ) {
        guard let interactor = interactor else {return };

        switch gestureState {
        case .began :
            interactor.hasStarted = true;
             interactor.shouldFinish = false;
            triggerSegue();
        case .changed :
            interactor.shouldFinish = progress > percentThreshold;
            interactor.update(progress);
        case .cancelled :
            interactor.hasStarted = false;
             interactor.shouldFinish = false;
            interactor.cancel();
        case .ended :
            interactor.hasStarted = false;
            interactor.shouldFinish
            ? interactor.finish()
            : interactor.cancel();
             interactor.shouldFinish = false;
        default :
            break;

        }
    }
}

MenuNavigationController:

import Foundation
import UIKit

class MenuNavigationController: UINavigationController, UINavigationControllerDelegate {

    let interactor = Interactor()

    override func viewDidLoad() {
        super.viewDidLoad();

        self.delegate = self;
    }

    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if((toVC as? MenuViewController) != nil) {
            return PresentMenuAnimator();
        }
        else {
            return DismissMenuAnimator();
        }
    }


    func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return interactor.hasStarted ? interactor : nil;
    }


}

PresentMenuAnimator:

import UIKit

class PresentMenuAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.6;
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard
        let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
            else {return};
        let containerView = transitionContext.containerView;

        containerView.insertSubview(toVC.view, aboveSubview: fromVC.view);

        let snapshot = fromVC.view.snapshotView(afterScreenUpdates: false);
        snapshot?.tag = MenuHelper.snapshotNumber;
        snapshot?.isUserInteractionEnabled = false;
        snapshot?.layer.shadowOpacity = 0.7;
        containerView.insertSubview(snapshot!, aboveSubview: toVC.view);
        fromVC.view.isHidden = true;

        UIView.animate(withDuration: transitionDuration(using: transitionContext),
                       animations: {snapshot?.center.x+=UIScreen.main.bounds.width*MenuHelper.menuWith;},
                       completion: {_ in
                        fromVC.view.isHidden = false;
                        transitionContext.completeTransition(!transitionContext.transitionWasCancelled);}
        );
    }

}

DismissMenuAnimator:

import UIKit

class DismissMenuAnimator : NSObject {
}

extension DismissMenuAnimator : UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.6;
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard
            let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from),
            let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
            else {
                return
            }
        let containerView = transitionContext.containerView;


        let snapshot = containerView.viewWithTag(MenuHelper.snapshotNumber)

        UIView.animate(withDuration: transitionDuration(using: transitionContext),
            animations: {

                snapshot?.frame = CGRect(origin: CGPoint.zero, size: UIScreen.main.bounds.size)
            },
            completion: { _ in
                let didTransitionComplete = !transitionContext.transitionWasCancelled
                if didTransitionComplete {

                    containerView.insertSubview(toVC.view, aboveSubview: fromVC.view)
                    snapshot?.removeFromSuperview()
                }
                transitionContext.completeTransition(didTransitionComplete)
        }
        )
    }
}

2 个答案:

答案 0 :(得分:0)

可以知道动画是否被取消,并且可以从func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool)的{​​{1}}方法中捕获动画。

以下是关于如何执行此操作的代码片段:

UINavigationControllerDelegate

此方法可以放在func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { navigationController.transitionCoordinator?.notifyWhenInteractionEnds { context in if context.isCancelled { // The interactive back transition was cancelled } } } 中,您可以在MenuNavigationController中保留PresentMenuAnimator并告诉它已取消转换,然后删除暂停的快照。

答案 1 :(得分:0)

要解决此问题,我在PresentMenuAnimator中添加了一个验证,以检查动画是否已取消。 如果是,则删除UIView.Animate中的快照。