【问题标题】:Pan Gesture completion handler getting called immediately立即调用 Pan Gesture 完成处理程序
【发布时间】:2018-07-19 05:09:03
【问题描述】:

我正在为视图设置动画以在用户平移屏幕时移动。我保留了一个阈值,之后视图将动画到默认位置。

目前的问题是在持续时间之前调用了将视图重置到某个位置的动画方法的完成处理程序。动画似乎是突然发生的,而不是持续一段时间。

// Pan gesture selector method
@objc func panAction(for panGesture: UIPanGestureRecognizer) {
    switch panGesture.state {
    case .began:
    //Began code
    case changed:
    if (condition) {
       print("IF")
       //Change constraint constant of customView
       animate(view: customView)
    } else if (condition) {
       print("ELSE IF")
       //Change constraint constant of customView
       animate(view: customView)
    } else {
       //Change constraint constant of customView
       print("ELSE")
       view.layoutIfNeeded()
    }
    case .ended:
    //Ended code
    default:
    break
    }
}

动画方法:

func animate(view: UIView) {
    UIView.animate(withDuration: 3, delay: 0, options: .curveEaseOut, animations: {
        view.layoutIfNeeded()
    }, completion: { (finished) in
        if finished {
            flag = false
        }
    })
}

立即设置标志,而不是在 3 秒后设置。

平移和越过阈值时得到的 o/p。

否则

否则

否则

否则

如果

编辑:我是个白痴。我没有在 superView 上调用layoutIfNeeded()

【问题讨论】:

  • 请添加您有问题的代码
  • 我正在编辑我的问题。请立即检查。

标签: ios swift uipangesturerecognizer


【解决方案1】:

当手势发生时,您的手势会发送多个事件,并且当您多次调用 UIView.animate() 代码时,新值将取代之前的值。

尝试添加动画选项.beginFromCurrentState

UIView.animate(withDuration: 0.5, delay: 0, options: [.beginFromCurrentState,.allowAnimatedContent,.allowUserInteraction], animations: {
    view.layoutIfNeeded()
}) { (completed) in
    ...
}

并期望随着手势的进行,您的 completed 会被多次调用,且 completed == false。


编辑:您的问题也可能与在错误视图上调用 layoutIfNeeded() 有关,可能尝试在 viewController.view 上调用它?

【讨论】:

  • 我正在更改锚点的常量。我想即使我在外面打电话他们也能正常工作。
  • 即使布局事件与该特定视图没有关联?
  • 我刚试过你说的。这似乎不是问题。
  • 我完全理解你的意思。先生,这似乎不是问题。感谢您花时间解释:)
  • 这就是您需要弄清楚如何传递您想要完成的更改的地方,因此它们会像我之前提到的那样应用于动画块中
【解决方案2】:

我解决了这个问题。我没有在 superview 上调用 layoutIfNeeded

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2021-06-04
    • 2014-08-16
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多