Pan Gesture完成处理程序立即被调用

时间:2018-07-19 05:09:03

标签: ios swift uipangesturerecognizer

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

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

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

ELSE

ELSE

ELSE

ELSE

IF

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

2 个答案:

答案 0 :(得分:0)

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

尝试添加动画选项.beginFromCurrentState

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

并希望随着手势的进行,您的完成对象会被多次调用,而完成对象== false。


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

答案 1 :(得分:0)

我解决了这个问题。我没有在超级视图上调用layoutIfNeeded

相关问题