【问题标题】:Interactive transition not linear交互式过渡不是线性的
【发布时间】:2017-08-11 18:02:47
【问题描述】:

我正在使用UIPercentDrivenInteractiveTransition 玩自定义和交互式视图控制器转换。我正在构建一个以模态方式呈现卡片(其他视图控制器)的应用程序。我已经使用UIViewControllerAnimatedTransitioning 进行了自定义转换,它为卡片视图控制器设置了动画,很像标准模型演示样式。

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    {...}
    let fullScreenFrame = transitionContext.finalFrame(for: cardVC)
    let offsetFrame = cardVC.view.frame.offsetBy(dx: 0, dy: cardVC.view.frame.height)
    let finalFrame = presenting ? fullScreenFrame : offsetFrame
    cardVC.view.frame = presenting ? offsetFrame : fullScreenFrame
    containerView.addSubview(cardVC.view)

    UIView.animateKeyframes(
        withDuration: transitionDuration(using: transitionContext),
        delay: 0,
        options: UIViewKeyframeAnimationOptions.calculationModeLinear,
        animations: {
            UIView.addKeyframe(
                withRelativeStartTime: 0,
                relativeDuration: 1,
                animations: { [weak self] in
                    guard let strongSelf = self else { return }
                    backgroundView.alpha = strongSelf.presenting ? 1 : 0
            })

            UIView.addKeyframe(
                withRelativeStartTime: 0,
                relativeDuration: 1,
                animations: {
                    cardVC.view.frame = finalFrame
            })
    }, completion: { finished in
        backgroundView.removeFromSuperview()
        gradientView.alpha = 1
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    })
}

然后我使用平移手势识别器以交互方式驱动关闭动画:

func handleGesture(_ gestureRecognizer: UIScreenEdgePanGestureRecognizer) {
        let translation = gestureRecognizer.translation(in: gestureRecognizer.view)
        var progress = (translation.y / (UIScreen.main.bounds.height - 70))
        progress = CGFloat(fminf(fmaxf(Float(progress), 0.0), 1.0))

        switch gestureRecognizer.state {
        case .began:
            interactionInProgress = true
            viewController.dismiss(animated: true, completion: nil)

        case .changed:
            shouldCompleteTransition = progress > 0.35
            update(progress)

        case .cancelled:
            interactionInProgress = false
            cancel()

        case .ended:
            interactionInProgress = false
            if !shouldCompleteTransition {
                cancel()
            } else {
                finish()
            }

        default:
            print("Unsupported")
        }
    }

当我通过向下拖动呈现的视图控制器来关闭它时,它似乎并没有随着手势线性移动。似乎交互控制器正在使用一些 easeInEaseOut 功能。也许设置UIPercentDrivenInteractiveTransitiontimingCurve 以使过渡线性运行。这是可能的,还是我搞错了?

【问题讨论】:

    标签: ios swift uiviewanimation interactive transitions


    【解决方案1】:

    我遇到了同样的问题,能够使用 UIView.setAnimationCurveanimateKeyframes 设置自定义动画曲线,如下所示:

    UIView.animateKeyframes(withDuration: 4, delay: 0, options: [], animations: {
    
        UIView.setAnimationCurve(.linear)
    
        UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.24, animations: {
            // Animations...
        })
    
        // Other key frames...
    })
    

    【讨论】:

    • 是的,就是这样!不知道为什么,但使用 UIPercentDrivenInteractiveTransition 默认情况下会给出一个真正“有弹性”的插值。尽管文档说它的曲线在交互时是线性的。无论如何,谢谢你
    【解决方案2】:

    您自己已经给动画一个默认的缓入-缓出时序曲线,在您对UIView.animateKeyframes 的调用中没有将时序曲线设置为任何不同的值。这甚至在交互过程中也适用。

    请注意,将动画的 options 设置为 UIViewKeyframeAnimationOptions.calculationModeLinear 并不会更改时序曲线(如果您认为这是您在此处完成的操作)。线性计算模式关键帧动画添加线性时序曲线的方法是这样的:

    var opts : UIViewKeyframeAnimationOptions = .calculationModeLinear
    let opt2 : UIViewAnimationOptions = .curveLinear
    opts.insert(UIViewKeyframeAnimationOptions(rawValue:opt2.rawValue))
    // and now use `opts` as your `options`
    

    【讨论】:

      【解决方案3】:

      在我的情况下,它适用于interruptibleAnimator(using:),返回UIViewPropertyAnimator

      【讨论】:

        猜你喜欢
        • 2016-06-01
        • 2019-04-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多