Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何添加屏幕边缘平移手势识别器来处理此pop动画?_Ios_Swift_Uinavigationcontroller - Fatal编程技术网

Ios 如何添加屏幕边缘平移手势识别器来处理此pop动画?

Ios 如何添加屏幕边缘平移手势识别器来处理此pop动画?,ios,swift,uinavigationcontroller,Ios,Swift,Uinavigationcontroller,我将UINavigationController子类化,并创建了一个处理推送和弹出转换的非交互式动画对象。要完成此操作,我如何添加UIScreenedGePangestureRecognitor来处理下面的自定义pop动画?我有点不知道从这里到哪里去,所以任何帮助都会很好,谢谢 class CustomNavigationController: UINavigationController { override func viewDidLoad() { super.vi

我将
UINavigationController
子类化,并创建了一个处理推送和弹出转换的非交互式动画对象。要完成此操作,我如何添加
UIScreenedGePangestureRecognitor
来处理下面的自定义pop动画?我有点不知道从这里到哪里去,所以任何帮助都会很好,谢谢

class CustomNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        delegate = self
        isNavigationBarHidden = true

    }

}

extension CustomNavigationController: UINavigationControllerDelegate {

    // noninteractive animator objects
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        switch operation {

        case .push:
            return CustomPushAnimator()

        case .pop:
            return CustomPopAnimator()

        default:
            return nil

        }

    }

    // the interactive animator controller
    func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {

        switch animationController {

        case is CustomPopAnimator:
            return CustomInteractiveController()

        default:
            return nil

    }

}

// the noninteractive push animator
class CustomPushAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    // push transition duration
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {

        return 0.2

    }

    // push transition animation
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let viewWidth = UIScreen.main.bounds.width
        let viewHeight = UIScreen.main.bounds.height
        let containerView = transitionContext.containerView
        let toViewController = transitionContext.viewController(forKey: .to)

        containerView.addSubview((toViewController?.view)!)
        toViewController?.view.frame = CGRect(x: viewWidth, y: 0, width: viewWidth, height: viewHeight)

        UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
            toViewController?.view.frame = CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight)
        }, completion: { finished in
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        })

    }

}

// the noninteractive pop animator
class CustomPopAnimator: NSObject, UIViewControllerAnimatedTransitioning {

    // pop transition duration
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {

        return 0.2

    }

    // pop transition animation
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let viewWidth = UIScreen.main.bounds.width
        let viewHeight = UIScreen.main.bounds.height
        let containerView = transitionContext.containerView
        let fromViewController = transitionContext.viewController(forKey: .from)
        let toViewController = transitionContext.viewController(forKey: .to)

        containerView.insertSubview((toViewController?.view)!, belowSubview: (fromViewController?.view)!)

        UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
            fromViewController?.view.frame = CGRect(x: viewWidth, y: 0, width: viewWidth, height: viewHeight)
        }, completion: { finished in
            fromViewController?.view.removeFromSuperview()
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        })

    }

}

class CustomInteractiveController: NSObject, UIViewControllerInteractiveTransitioning {

    func startInteractiveTransition(_ transitionContext: UIViewControllerContextTransitioning) {

        // I think the edge screen pan gesture goes here? Again, any advice is greatly appreciated!

    }

}

通常,编写交互式自定义过渡动画的过程是:

  • 在转换开始之前,必须为负责转换的视图控制器指定一个委托

  • 您将拥有一个跟踪交互手势的手势识别器。当手势识别器识别时,它会触发到新视图控制器的转换

  • 过渡开始时,将要求学员提供动画控制器。您将返回UIViewControllerAnimatedTransitioning对象

  • 学员还需要一个交互控制器。您将返回UIViewControllerInteractiveTransitioning对象(或
    nil
    ,以防止转换是交互式的)。此对象实现
    startinteractivatetransition(\uo:)

  • 手势识别器继续不断地在转换上下文上调用
    updateInteractiveTransition(:)
    ,以及管理动画的帧

  • 这种姿态迟早会结束。此时,我们必须决定是宣布过渡完成还是取消。一种典型的方法是,如果用户完成了一半以上的完整手势,则构成完成;否则,即构成撤销。我们相应地完成动画

  • 动画现在已完成,并调用其完成函数。我们必须调用转换上下文的
    finishInteractiveTransition
    cancelInteractiveTransition
    ,然后调用其
    CompletTransition(:)
    ,并使用一个参数说明转换是完成还是取消

  • 我们的
    animationEnded
    被调用,我们清理


  • 过去,您可以使用UIPercentDrivenTerractiveTransition对象来帮助您,但现在(iOS 10及更高版本)我的自定义转换动画总是使用UIViewPropertyAnimator;要做到这一点,首先必须重写整个代码。我通常保留三个实例属性:UIViewPropertyAnimator、转换上下文(因为手势识别器需要一个引用),以及一个Bool标志,表明这是否是一个交互式动画(因为同一代码可以重复用于同一动画的交互式和非交互式版本).

    对于否决这一问题的人,我希望能给他一个理由。我花了很多时间来研究和编写这段代码,我问了一个非常具体的问题,给出了什么?你找到了如何将它们联系在一起的答案了吗?我也找不到。我采纳了你的建议,使用
    UIViewPropertyAnimator
    重构了动画。我通过编辑创建了一个要点,但不知道如何将平移手势识别器和处理程序链接到交互式控制器(在底部)。缺失的环节是什么?使用
    uipercentdriventeractivatetransition
    (我过去做过,但因为0.999黑客攻击而讨厌)有很多教程,但没有找到使用
    UIViewPropertyAnimator
    的教程(显然是因为它在iOS 10中直到现在都不存在)。如果你知道我是谁,你知道我的书代码在哪里,你可以找到这本书的示例代码,它演示了我在回答中所说的。