用于NSLayoutConstraints的UIViewPropertyAnimator使视图消失

时间:2019-02-03 04:44:05

标签: ios swift uiviewpropertyanimator

我有一个viewDidAppear中这样设置的UIViewPropertyAnimator:

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会根据用户在drawerChangedDistanceFromBottom函数中将抽屉向上移动的距离进行更新。 Pulley库是这样的:

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

所描述的过程按预期进行,直到用户进入后台并返回到应用程序为止。在这一点上,我所表达的带有约束的观点似乎完全消失了。我看到了另一个问题,该问题指示UIViewPropertyAnimator does strange things with the lifecycle。也许是这样的问题引起的?

我也从不启动/完成动画师,我只是在fractionComplete中更新了drawerChangedDistanceFromBottom,也许我应该吗?尽管我不希望该动画实际上持续5秒钟,但我只希望可擦除的动画。

1 个答案:

答案 0 :(得分:0)

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

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