我有自定义 tapbar,其中包含 collectionView 和 4 个项目。我有白色 View ,它指示所选项目并在点击另一个 collectionView 项目时更改其位置(带动画)。带约束的白色 View 定位,我将左 anchor 约束更改为动画
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let x = CGFloat(indexPath.item) * frame.width / 4
self.horizontalBarLeftAnchorConstraint?.constant = x
UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
self.layoutIfNeeded()
}, completion: nil)
}
动画无法正常工作,一开始它会跳转到当前位置之前 1 个宽度(白色 View )的位置,并从该位置开始动画
我发现了问题// 我有这个约束的另一个动画来响应滚动 Collection View ,这里我有产生这种跳跃的双重动画,我删除了第二个动画,现在一切看起来都很好
最佳答案
你没有设置任何动画条件,也许我不明白你的问题。但是你应该使用:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// BEFORE ANIMATION CONTEXT
UIView.animate(withDuration: 0.75, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
// ANIMATION TILL END CONTEXT
self.layoutIfNeeded
}, completion: {
// AFTER ANIMATION CONTEXT
})
}
关于ios - swift 4 约束动画无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50677207/