ios - NSLayoutConstraints 的 UIViewPropertyAnimator 使 View 消失

标签 ios swift uiviewpropertyanimator

我有一个 UIViewPropertyAnimator,我在 viewDidAppear 中设置如下:

if animator == nil {
    animator = UIViewPropertyAnimator(duration: 5.0, curve: .easeInOut, animations: { [weak self] in
        guard let blockSelf = self else { return }
        blockSelf.primaryPromptLabel.alpha = 0
        blockSelf.secondaryPromptLabel.alpha = 0
        blockSelf.backArrowButton.alpha = 1
        NSLayoutConstraint.activate(blockSelf.constraintsToAdd ?? [])
        NSLayoutConstraint.deactivate(blockSelf.constraintsToRemove ?? [])
        blockSelf.view.layoutIfNeeded()
    })
}

我正在使用动画器在两个互斥的约束集之间进行插值。我正在使用图书馆 Pulley对于抽屉,当拖动抽屉时,UIViewPropertyAnimator 的 fractionComplete 会根据用户在 Pulley 库的 drawerChangedDistanceFromBottom 函数中向上移动抽屉的距离进行更新,例如这个:

// Extension to clamp a value within bounds
extension ClosedRange {
    func clamp(_ value : Bound) -> Bound {
        return self.lowerBound > value ? self.lowerBound
            : self.upperBound < value ? self.upperBound
            : value
    }
}

// PulleyDrawerViewControllerDelegate in my view controller
func drawerChangedDistanceFromBottom(drawer: PulleyViewController, distance: CGFloat, bottomSafeArea: CGFloat) {
    let bottomSafeSpace = drawer.bottomSafeSpace
    let expandedHeight = drawer.visibleDrawerHeight
    let partialHeight = drawer.partialRevealDrawerHeight(bottomSafeArea: bottomSafeSpace)

    let percentExpanded = (0.0 ... 1.0).clamp(1 * (drawer.drawerDistanceFromBottom.distance-partialHeight) / (expandedHeight-partialHeight))

    animator?.fractionComplete = percentExpanded
}

所描述的过程正在按预期工作,直到用户转到后台并返回到应用程序。到那时,我在约束下动画的 View 似乎就消失了。我看到另一个问题表明 UIViewPropertyAnimator does strange things with the lifecycle 。也许类似的事情导致了问题?

我也从不开始/完成动画师,我只是更新drawerChangedDistanceFromBottom中的fractionComplete,也许我应该?虽然我不希望动画实际运行 5 秒的持续时间,但我只想要一个可擦洗的动画。

最佳答案

我不确定问题的原因,但我添加的约束之一是主标签和辅助标签的 height = 0 约束。标签也在堆栈 View 中。我从堆栈 View 中删除了标签,但仍然遇到同样的问题。目前,我通过将动画更改为 CGAffineTransform 进行翻译而不是添加 height = 0 约束来解决这个问题。当使用 CGAffineTransform 进行缩放时,它不起作用(无论我是否将标签放在堆栈 View 中)。

如果有人有更好的建议,请告诉我。

关于ios - NSLayoutConstraints 的 UIViewPropertyAnimator 使 View 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54500026/

相关文章:

ios - SVProgressHud 环形/前景色 IOS

ios - UIPercentDrivenInteractiveTransition 取消问题

ios - 扩展 UIViewPropertyAnimator?

ios - 从 UITapGestureRecognizer 中查找被点击的 UIImageview

ios - 如何关闭模态视图 Controller ,然后立即让呈现 View Controller 呈现不同的模态视图 Controller ?

ios - googledrivesdk和文件夹的处理

Swift 5 调用 squareRoot()

ios - 如何在 Swift 中获取和设置 Http Header?

ios - Travis CI 上的 Carthage 缓存

ios - 将设置 UIView 属性显式停止/完成在同一属性上使用 UIViewPropertyAnimator 启动的现有动画