Skip to content

The card gets stuck at the top of the view when sliding quickly #153

Open
@MingYik

Description

@MingYik

The card gets stuck at the top of the view when sliding quickly, Here is the fix code:

@objc func handlePan(_ recognizer: UIPanGestureRecognizer) {

        guard let swipeableView = swipeableView else { return }
        let translation = recognizer.translation(in: containerView)
        let location = recognizer.location(in: containerView)
        let velocity = recognizer.velocity(in: containerView)
        let movement = Movement(location: location, translation: translation, velocity: velocity)
        switch recognizer.state {
        case .began:
            guard case .snapping(_) = state else { return }
            state = .moving(location)
            swipeableView.didStart?(view, location)
        case .changed:
            guard case .moving(_) = state else { return }
            state = .moving(location)
            swipeableView.swiping?(view, location, translation)
        case .ended, .cancelled:
            guard case .moving(_) = state else { return }
            if swipeableView.shouldSwipeView(view, movement, swipeableView) {
                let directionVector = CGVector(point: translation.normalized * max(velocity.magnitude, swipeableView.minVelocityInPointPerSecond))
                // The reason is that fast sliding will have a directionVector of Nan, and the relevant judgment is added here
                guard directionVector.dx.isNaN || directionVector.dy.isNaN else {
                    state = .swiping(location, directionVector)
                    swipeableView.swipeView(view, location: location, directionVector: directionVector)
                    swipeableView.didEnd?(view, location)
                    return
                }
            }
            state = snappingStateAtContainerCenter()
            swipeableView.didCancel?(view)
            swipeableView.didEnd?(view, location)
        default:
            break
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions