我遇到了一些困難動畫我的觀點的layoutConstraints之一:UIView的動畫而不考慮時間瞬間完成或延緩
UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.topLayoutConstraint?.constant = self.currentYPosition
self.layoutIfNeeded()
}, completion: nil)
無論我使用的持續時間或延遲,動畫瞬間完成(我的觀點從之前的位置跳到新的位置)。
我檢查了所有的值,他們是正確的。 我也檢查過,並調用動畫和完成。
任何想法?
編輯:
@objc private func viewDragged(_ recognizer: UIPanGestureRecognizer) {
let location = recognizer.location(in: self.superview)
switch recognizer.state {
case .ended:
if (recognizer.direction == .Down) { // Collapsing the slide panel
currentYPosition = parentViewHeight - minimumVisibleHeight - statusBarHeight - navBarHeight
}
else if (recognizer.direction == .Up) { // Expanding the slide panel
// Substracting `minimumVisibleHeight` so that the dragable part is hidden behind the navigation bar
currentYPosition = -minimumVisibleHeight
}
self.topLayoutConstraint?.constant = self.currentYPosition
UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
self.layoutIfNeeded()
}, completion: nil)
break
case .began:
HomeSlidePanelContainerView.draggingPreviousPosition = location.y
default:
self.layoutIfNeeded()
if (location.y < (parentViewHeight - minimumVisibleHeight - statusBarHeight - navBarHeight)
|| location.y > (statusBarHeight + navBarHeight)) {
currentYPosition -= HomeSlidePanelContainerView.draggingPreviousPosition - location.y
topLayoutConstraint?.constant = currentYPosition
self.layoutIfNeeded()
}
HomeSlidePanelContainerView.draggingPreviousPosition = location.y
break
}
}
您是否嘗試在'layoutIfNeeded()'之前調用'setNeedsUpdateConstraints()'? – werediver
@werediver喜歡在動畫還是之前?在這兩種情況下,nop我沒有 – Moucheg
把這行'self.topLayoutConstraint?.constant = self.currentYPosition'移出動畫塊。 – holex