【问题标题】:Swift : UIPercentDrivenInteractiveTransition on cancel?斯威夫特:UIPercentDrivenInteractiveTransition 取消?
【发布时间】:2017-07-13 09:03:58
【问题描述】:

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

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

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

现在,由于我使用的是带有 show 的 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;

        }
    }
}

菜单导航控制器:

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

【问题讨论】:

    标签: ios swift uinavigationcontroller delegates


    【解决方案1】:

    可以知道动画是否被取消,可以在UINavigationControllerDelegatefunc navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool)方法中捕获。

    这里有一段代码说明如何做到这一点:

    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 并告诉它转换被取消,并在那里删除挂起的快照。

    【讨论】:

    • 对不起,它不起作用,因为手势是取消但视图仍然落后。所以事件没有被触发。
    • 从那以后我不知何故错过了这一点,但我对哪些事件没有被触发感到困惑?如果你不记得没关系,只是当时很好奇。当时我也遇到了同样的问题,我认为这很有效
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-07-22
      • 2017-03-24
      • 1970-01-01
      • 2016-02-08
      • 2017-11-13
      • 2015-08-19
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多