代码之家  ›  专栏  ›  技术社区  ›  Yuriy Fedchenko

swift 4约束动画无法正常工作

  •  0
  • Yuriy Fedchenko  · 技术社区  · 6 年前

    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)
        }
    

    动画无法正常工作,在开始时,将其jums放置在当前位置之前1个宽度(白色视图)上,然后从该位置开始动画

    我发现了问题// 我有另一个关于这个约束的动画来响应滚动集合视图,这里我有两个动画,产生了这样的跳跃,我删除了第二个动画,现在一切看起来都很好

    2 回复  |  直到 6 年前
        1
  •  1
  •   Achron    6 年前

    你没有设置任何动画条件,也许我不明白你的问题。但您应该使用:

    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
    
        })
    }
    
        2
  •  0
  •   Mahendra    6 年前
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {
    
           let x = CGFloat(indexPath.item) * frame.width / 4
           self.horizontalBarLeftAnchorConstraint?.constant = x
            self.layoutIfNeeded()
        }, completion: nil)
    }