由于暂停动画和 NSInternalInconsistencyException 导致应用程序崩溃

The*_*uss 6 animation uikit ios swift

我通过向上和向下滑动来在屏幕上和屏幕外设置一个 UIView 动画。我使用此代码来做到这一点:

private var _animator: AnyObject?
@available(iOS 10.0, *)
var animator: UIViewPropertyAnimator? {
    get {
        return _animator as? UIViewPropertyAnimator
    }
    set {
        _animator = newValue
    }
}
var haveTheySwiped = false
var haveTheyNotSwiped = true

@available(iOS 10.0, *)
func handlePan(recognizer: UIPanGestureRecognizer) {
    let vel = recognizer.velocity(in: self)
        switch recognizer.state {
        case .began:
            if vel.y < 0 && !haveTheySwiped {
                animator = UIViewPropertyAnimator(duration: 1, curve: .easeOut, animations: {
                    self.backgroundImage.frame = self.backgroundImage.frame.offsetBy(dx: 0, dy: -603)
                })
                haveTheySwiped = true
                isScanVisible = true
            } else if vel.y > 0 && haveTheySwiped {
                animator = UIViewPropertyAnimator(duration: 1, curve: .easeOut, animations: {
                    self.backgroundImage.frame = self.backgroundImage.frame.offsetBy(dx: 0, dy: 603)
                })
                haveTheySwiped = false
                isScanVisible = false
            }
            animator?.pauseAnimation()
            print(backgroundImage.frame.origin)
        case .changed:
            let translation = recognizer.translation(in: backgroundImage)
            if vel.y < 0 {
                animator?.fractionComplete = translation.y / -603
            } else if vel.y > 0 && haveTheySwiped == true {
                animator?.fractionComplete = translation.y / 603
            }
        case .ended:
            animator?.continueAnimation(withTimingParameters: nil, durationFactor: 0)
        case .possible: break
        default: break
        }
}
Run Code Online (Sandbox Code Playgroud)

但是,最近我遇到了一个错误。当我尝试拉起时,UIView 没有出现,然后当我抬起手指时,应用程序崩溃并出现以下错误:

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“释放暂停或停止的属性动画器是错误的。属性动画师必须完成动画或明确停止并完成才能释放它们。

有没有人遇到过这个?希望得到一些帮助,找出导致这种情况发生的原因。任何帮助将不胜感激。非常感谢!

干杯,托

Edo*_*ier 16

如果有人在 2019 年或更晚的时候仍然遇到这种崩溃,我的动画师也遇到了类似的问题,我只需要运行其完成部分的 25%(以在 collectionView 单元格上创建微妙的模糊)。

当单元格被 deinit 或 VC 从 navController 弹出时,就会发生崩溃。

在我的单元格的 UI 设置中,我有以下实现

之前:导致崩溃

 animator?.fractionComplete = 0.25
Run Code Online (Sandbox Code Playgroud)

之后:不再崩溃

 animator?.fractionComplete = 0.25
 animator?.stopAnimation(true) 
 animator?.finishAnimation(at: .current)
Run Code Online (Sandbox Code Playgroud)

这对我有用。


sol*_*oid 3

实施 stopAnimation(Bool) 然后 finishAnimation(at: UIViewAnimatingPosition) 应该可以解决问题。

此类采用 UIViewAnimating 和 UIViewImplicitlyAnimating 协议,定义启动、停止和修改动画的方法。有关这些协议的方法的更多信息,请参阅 UIViewAnimating 和 UIViewImplicitlyAnimating 。

https://developer.apple.com/documentation/uikit/uiviewanimating

func stopAnimation(Bool) 将动画停止在当前位置。必需的。

func finishAnimation(at: UIViewAnimatingPosition) 完成动画并将动画器返回到非活动状态。必需的。