UIViewPropertyAnimator AutoLayout完成问题?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (116)

我正在使用UIViewPropertyAnimator来运行数组交互式动画,我遇到的一个问题是,每当我反转动画时,我都无法再向后运行动画。

我正在使用三个函数来结合平移手势识别器来处理动画。

private var runningAnimations = [UIViewPropertyAnimator]()

private func startInteractiveTransition(gestureRecognizer: UIPanGestureRecognizer, state: ForegroundState, duration: TimeInterval) {

    if runningAnimations.isEmpty {
        animateTransitionIfNeeded(gestureRecognizer: gestureRecognizer, state: state, duration: duration)
    }

    for animator in runningAnimations {
        animator.pauseAnimation()
        animationProgressWhenInterrupted = animator.fractionComplete
    }
}

private func animateTransitionIfNeeded(gestureRecognizer: UIPanGestureRecognizer, state: ForegroundState, duration: TimeInterval) {

    guard runningAnimations.isEmpty else {
        return
    }

    let frameAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1) {

        switch state {
        case .expanded:
            // change frame
        case .collapsed:
            // change frame
        }
    }

    frameAnimator.isReversed = false

    frameAnimator.addCompletion { _ in
        print("remove all animations")
        self.runningAnimations.removeAll()
    }

    self.runningAnimations.append(frameAnimator)

    for animator in runningAnimations {
        animator.startAnimation()
    }
}

private func updateInteractiveTransition(gestureRecognizer: UIPanGestureRecognizer, fractionComplete: CGFloat) {

    if runningAnimations.isEmpty {
        print("empty")
    }
            for animator in runningAnimations {
        animator.fractionComplete = fractionComplete + animationProgressWhenInterrupted
    }
}

我注意到的是在我反转动画然后调用animateTransitionIfNeeded之后,frameAnimator被附加到运行的动画中,但是当我立即调用updateInteractiveTransition并检查runningAnimations时,它是空的。

所以我认为这可能与swift如何处理内存或者UIViewAnimating如何完成动画有关。

有什么建议么?

提问于
用户回答回答于

设置约束并调用view.layoutIfNeeded()

animator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1) {
        [unowned self] in

        switch state {
        case .expanded:
            self.constraintA.isActive = false
            self.constraintB.isActive = true
            self.view.layoutIfNeeded()
        case .collapsed:
            self.constraintB.isActive = false
            self.constraintA.isActive = true
            self.view.layoutIfNeeded()
        }
}

现在,由于我们的动画师有能力逆转,我们添加了一个完成处理程序,以确保正确的约束在完成时使用完成位置。

animator.addCompletion {  [weak self] (position) in

        if position == .start {
            switch state {
            case .collapsed:
                self?.constraintA.isActive = false
                self?.constraintB.isActive = true
                self?.view.layoutIfNeeded()
            case .expanded:
                self?.constraintA.isActive = false
                self?.constraintB.isActive = true
                self?.view.layoutIfNeeded()
            }
        }
}

扫码关注云+社区

领取腾讯云代金券