Preventing UICollectionViewCell animated appearance when presenting UICollectionView

前端 未结 2 1927
孤独总比滥情好
孤独总比滥情好 2020-12-30 13:38

When a user does some action, I need to pull a UICollectionView from the bottom up to a certain height. Since that new state is totally optional, the collection

相关标签:
2条回答
  • 2020-12-30 13:51

    I've been in this problem for around 3 days and finally got a solution. Simply I added the animation block to be called after a very tiny timer

    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 7 , options: .CurveEaseInOut, animations: {
                self.view.layoutIfNeeded()
            }){ _ in}
    

    will be

    let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
        dispatch_after(delayTime, dispatch_get_main_queue()) {
            self.changeConstraint()
        }
    
    private func changeConstraint(){
    
            UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 7 , options: .CurveEaseInOut, animations: {
                self.view.layoutIfNeeded()
            }){ _ in}
    }
    

    See the magic :)

    0 讨论(0)
  • 2020-12-30 14:07

    Expanding from the top left corner is typically a symptom of calling layoutIfNeeded in an animation block when the original view has never been laid out. You're basically animating the initial layout pass, where all subviews have started at CGRectZero.

    To solve it you need two things:

    • Ensure the constraint you're editing is related to the position of the collection view, not the size. By this I mean that you're not presenting your collection view by changing its height from zero to the final value.
    • Call layoutIfNeeded on your view before you edit the constraint, then call it again in the animation block after you've made the change. This way you're only animating the change you've specified to the constraint, rather than the entire layout.
    0 讨论(0)
提交回复
热议问题