【发布时间】:2016-11-08 16:18:25
【问题描述】:
我在为我的视图的 layoutConstraints 之一设置动画时遇到了一些困难:
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 喜欢在动画中还是之前?无论哪种情况,不,我没有
-
将
self.topLayoutConstraint?.constant = self.currentYPosition这一行从动画块中取出。 -
@holex 已经完成(见编辑),但没有任何变化
标签: ios swift uiviewanimation