如何预取屏幕上不可见的收藏夹视图单元格

时间:2019-02-07 14:35:04

标签: ios swift uicollectionview

我有一个collectionView,并且在整个视图中添加了Pan gesture。 在第一阶段,collectionView的高度是屏幕的3/4。当用户在屏幕上平移时,collectionView的高度会增加以适合整个屏幕,然后启用collectionView滚动。

(现在,collectionView的高度在所有东西都可以正常工作后第一次发生怪异的变化。)我认为问题是collectionViewCell不是第一次加载,因此加载需要时间。在collectionView正确加载之后,一切正常。

这是代码

@objc private func bottomViewPanned(recognizer: UIPanGestureRecognizer) {

    switch recognizer.state {
    case .began:

        self.animateTransitionIfNeeded(to: self.currentState.opposite, duration: 1)
        runningAnimators.forEach { $0.pauseAnimation() }
        animationProgress = runningAnimators.map { $0.fractionComplete }
    case .changed:
        let yVelocity = recognizer.velocity(in: profileCollectionView).y
        let shouldClose = yVelocity > 0

        let translation = recognizer.translation(in: profileCollectionView)
        var fraction = -translation.y / popupOffset
        if currentState == .open { fraction *= -1 }
        if runningAnimators[0].isReversed { fraction *= -1 }
        for (index, animator) in runningAnimators.enumerated() {
            animator.fractionComplete = fraction + animationProgress[index]
        }
    case .ended:
        let yVelocity = recognizer.velocity(in: profileCollectionView).y
        let shouldClose = yVelocity > 0
        if yVelocity == 0 {
            runningAnimators.forEach { $0.continueAnimation(withTimingParameters: nil, durationFactor: 0) }
            break
        }
        switch currentState {
        case .open:
            if !shouldClose && !runningAnimators[0].isReversed { runningAnimators.forEach { $0.isReversed = !$0.isReversed } }
            if shouldClose && runningAnimators[0].isReversed { runningAnimators.forEach { $0.isReversed = !$0.isReversed } }
        case .closed:
            if shouldClose && !runningAnimators[0].isReversed { runningAnimators.forEach { $0.isReversed = !$0.isReversed } }
            if !shouldClose && runningAnimators[0].isReversed { runningAnimators.forEach { $0.isReversed = !$0.isReversed } }

        }
        runningAnimators.forEach { $0.continueAnimation(withTimingParameters: nil, durationFactor: 0) }

    default:
        ()
    }
}


func animateTransitionIfNeeded(to state: state, duration: TimeInterval , panningDown : Bool = true) {

    guard runningAnimators.isEmpty else { return }
    let transitionAnimator = UIViewPropertyAnimator(duration: duration, dampingRatio: 1.0, animations: {
        switch state {

        case .open:
            self.topConstraint.constant = 0.0


        case .closed:
            self.topConstraint.constant = initialheight

        }
        self.view.layoutIfNeeded()
    })

    // the transition completion block
    transitionAnimator.addCompletion { position in

        // update the state
        switch position {
        case .start:
            self.currentState = state.opposite
        case .end:
            self.currentState = state
        case .current:
            ()
        }

        // manually reset the constraint positions
        switch self.currentState {
        case .open:

            self.topConstraint.constant = 0.0

        case .closed:
            self.topConstraint.constant = initialheight
        }
        self.runningAnimators.removeAll()
    }
    transitionAnimator.startAnimation()
    runningAnimators.append(transitionAnimator)
}

0 个答案:

没有答案