UIPercentDrivenInteractiveTransition取消问题

时间:2017-07-30 15:17:47

标签: ios swift xcode ios11 uiviewpropertyanimator

我有什么

我正在使用附加UIViewControllerAnimatedTransitioning的{​​{1}} protocol来平移以关闭视图控制器

UIViewPropertyAnimator

Pan Gesture处理动画:

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

什么有效

向下滑动以解雇工作完美无缺。轻轻向下滑动并抬起手指取消也可以很好地工作。

问题

向下滑动并向上滑动超出起点(进度变为负值)并抬起手指应该取消转换并取消动画。这在iOS 10中发生,但它首先反转导航控制器转换,然后快照。在iOS 11中,取消动画发生,然后我看到导航控制器转换是相反的。如果您等待,您可以看到导航控制器转换确实尝试在10分钟左右的动画中自行更正。

问题:

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

1 个答案:

答案 0 :(得分:2)

AFAIK这是一个UIKit错误,需要0.999黑客攻击。将.completionSpeed = 0.999添加到平移处理程序处理程序的interactionController案例中的.ended