12

我有的

我正在使用UIViewControllerAnimatedTransitioning protocol附加UIViewPropertyAnimator的平移来关闭视图控制器

extension SecondViewController : UIViewControllerAnimatedTransitioning {
    func interruptibleAnimator(using ctx: UIViewControllerContextTransitioning) -> UIViewImplicitlyAnimating {

        if self.animator != nil {
            return self.animator!
        }

        let containerView = ctx.containerView
        let toVC = ctx.viewController(forKey: .to) as! FirstViewController
        let fromVC = ctx.viewController(forKey: .from) as! SecondViewController

        containerView.insertSubview(toVC.view, belowSubview: fromVC.view)

        self.animator = UIViewPropertyAnimator(duration: transitionDuration(using: ctx),
                                          curve: .easeOut, animations: {

            self.fromVC.view.transform = CGAffineTransform(scale: 0.5)

        })

        self.animator.isInterruptible = true
        self.animator.isUserInteractionEnabled = true
        self.animator.isManualHitTestingEnabled = true

        self.animator.addCompletion { position in
            switch position {
            case .end:
                break
            case .current:
                break
            case .start:
                break
            }
            let cancelled = ctx.transitionWasCancelled
            if (cancelled) {
                //..
            } else {
                //..
            }
            ctx.completeTransition(!cancelled)
        }
        self.animator = anim
        return self.animator
    }

    func transitionDuration(using ctx: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }

    func animateTransition(using ctx: UIViewControllerContextTransitioning) {
        let animator = self.interruptibleAnimator(using: ctx)
        self.animator.startAnimation()
    }

    func animationEnded(_ transitionCompleted: Bool) {
        self.interactiveTransition = nil
        self.animator = nil
    }
}

平移手势来处理动画:

func handlePanGesture(gestureRecognizer: UIPanGestureRecognizer) {

    let panTranslation = gestureRecognizer.translation(in: gestureRecognizer.view!)
    var progress = panTranslation.y / (gestureRecognizer.view!.bounds.size.height * 0.5)

    switch gestureRecognizer.state {
    case .began:

        self.interactiveTransition = UIPercentDrivenInteractiveTransition()
        self.navigationController!.popViewController(animated: true)

    case .changed:
        self.interactiveTransition!.update(progress)

    case .cancelled, .ended:

        if progress > 0.5 {
            //Complete Transition
            let timingParameters = UICubicTimingParameters(animationCurve: .easeInOut)
            self.animator!.continueAnimation!(withTimingParameters: timingParameters, durationFactor: progress)
            self.animator?.addAnimations! {
                //Completion Animations

            }
            self.interactiveTransition!.finish()

        } else {
            //Cancel Transition
            self.animator!.isReversed = true
            let timingParameters = UICubicTimingParameters(animationCurve: .easeInOut)
            self.animator!.continueAnimation!(withTimingParameters: timingParameters, durationFactor: progress)
            self.animator!.addAnimations!({
                //Cancelling Animations

            }, delayFactor: 0 )
            self.interactiveTransition!.cancel()
        }

    default:
        break
    }
}

什么有效

向下滑动以解雇工作完美。轻轻向下滑动并抬起手指以取消也非常有效。

问题

向下和向后滑动超出起点(进度变为负数)并抬起手指应该取消动画并取消过渡。这发生在 iOS 10 中,但它首先会先反转导航控制器的转换,然后再回弹。在 iOS 11 中,取消动画发生,然后我看到导航控制器转换反转。如果您等待,您会看到导航控制器转换确实会尝试在 10 分钟左右的动画中自行纠正它。

问题:

 - self.interactiveTransition!.cancel()?
 - self.interactiveTransition!.completionSpeed ??
4

1 回答 1

3

我不知道这是一个错误还是我们都做错了,但要纠正行为,请添加.completionSpeed = 0.999到平移手势处理程序interactionController的情况下。.ended这是一个 hack,但至少它只有一行。

于 2017-12-14T18:34:06.380 回答