gpt4 book ai didi

swift - UIViewPropertyAnimator AutoLayout 完成问题

转载 作者:行者123 更新时间:2023-11-28 11:53:38 27 4
gpt4 key购买 nike

我正在使用 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 如何完成动画有关。

有什么建议吗?

最佳答案

我开始意识到我遇到的问题是 UIViewPropertyAnimator 如何在反转时处理布局约束。我无法在网上或官方文档中找到更多详细信息,但我确实发现这很有帮助。

Animator just animates views into new frames. However, reversed or not, the new constraints still hold regardless of whether you reversed the animator or not. Therefore after the animator finishes, if later autolayout again lays out views, I would expect the views to go into places set by currently active constraints. Simply said: The animator animates frame changes, but not constraints themselves. That means reversing animator reverses frames, but it does not reverse constraints - as soon as autolayout does another layout cycle, they will be again applied.

像往常一样设置约束并调用 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()
}
}
}

关于swift - UIViewPropertyAnimator AutoLayout 完成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51485746/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com