ios - swift : UIPercentDrivenInteractiveTransition on cancel?

标签 ios swift uinavigationcontroller delegates

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

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

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

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

我将 UIViewControllerTransitioningDelegate 换成了 UINavigationControllerDelegate,这样我就可以覆盖 navigationController 函数。

这意味着我可以取出抽屉并关闭它。它适用于按钮或手势。 我的问题如下:如果我没有完成将抽屉拖动到足够远以使其达到阈值并完成动画,它将被取消并隐藏。这一切都很好,但是当发生这种情况时,不会调用关闭函数,这意味着我在 PresentMenuAnimator 中放置的快照仍然在所有层的前面,我什至被困在那里尽管我可以与它背后的东西互动。

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

交互者:

import UIKit


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

}

菜单助手:

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;

        }
    }
}

菜单导航 Controller :

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);}
        );
    }

}

关闭菜单动画器:

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)
        }
        )
    }
}

最佳答案

可以知道动画是否被取消,可以在func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool)方法中捕获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 并告诉它转换已取消,并在其中删除挂起的快照周围。

关于ios - swift : UIPercentDrivenInteractiveTransition on cancel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45076085/

相关文章:

ios - ScrollView 将内容移出 View

ios - 设置UIImageView图像后多次调用CFRunLoopRun

javascript - Facebook SDK 和自定义按钮登录 react-native 0.60+ 不工作

iOS Firebase/Firestore 权限被拒绝

iphone - iOS Storyboard如何在没有导航 Controller 的情况下使用 "push"样式从右侧呈现 Controller ?

iOS 13 UIBarButtonItem 使用 UISearchController 时不可点击且重叠 UINavigationBars

ios - 在 iOS Swift 中,在 Swift 4 中的 TableView 内集成 CollectionView 的不同数组

enums - swift 。 UILabel 文本对齐

ios - 当它们是两个 View 之间的导航 Controller 时,如何将 managedObjectContext 从 appDelegate 传递到第一个 ViewController

swift - "Back"按钮后出现意外行为