JoniVR / VerticalCardSwiper

A marriage between the Shazam Discover UI and Tinder, built with UICollectionView in Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do I prevent my view controller from loading twice when I use the didDragCard function?

tracknemo opened this issue · comments

New Issue Checklist

Question

Hi, I am overriding the didDragCard method and I essentially want a custom view controller I created to display upon dragging the card to the left. The issue arises after swiping the card, where the view controller is being pushed twice rather than only once. Here is my code:
`func didDragCard(card: CardCell, index: Int, swipeDirection: SwipeDirection) {

    let info = myArr[index]
    switch swipeDirection{
    case SwipeDirection.Left:
        let myVC = AccountOverviewVC(songType: info.songInfo, userId: info.uid)
        self.navigationController?.pushViewController(myVC, animated: false)
    default:
        print("do nothing because be we didn't drag left")
    }
}`

As mentioned, the function works although the view controller is pushed onto my view twice. I noticed that when I lightly drag the card (barely even dragging it) the expected behavior occurs (the view is only pushed once). But if I do a long drag in the left direction, the view is pushed multiple times.

Hi,

This is intended behaviour. The didDragCard method is called while you're dragging a card, so it's supposed to be called repeatedly.

Depending on what you want to achieve exactly, you could either use the willSwipeCardAway or didSwipeCardAway delegate method or simply disable side swiping after the first call of didDragCard (cardSwiper.isSideSwipingEnabled = false).

I could perhaps implement something like a didChangeSwipeDirection delegate method but that would be called instantly once you start dragging, so it'd achieve the exact same result you'd get simply using the didDragCard method I described above.

My guess would be your best bet is to use either the willSwipeCardAway or didSwipeCardAway method.

If I'm overlooking something here, let me know what it is you want to achieve (aka when exactly you want to receive a dragging notification or what you're missing).

Hi,

This is intended behaviour. The didDragCard method is called while you're dragging a card, so it's supposed to be called repeatedly.

Depending on what you want to achieve exactly, you could either use the willSwipeCardAway or didSwipeCardAway delegate method or simply disable side swiping after the first call of didDragCard (cardSwiper.isSideSwipingEnabled = false).

I could perhaps implement something like a didChangeSwipeDirection delegate method but that would be called instantly once you start dragging, so it'd achieve the exact same result you'd get simply using the didDragCard method I described above.

My guess would be your best bet is to use either the willSwipeCardAway or didSwipeCardAway method.

If I'm overlooking something here, let me know what it is you want to achieve (aka when exactly you want to receive a dragging notification or what you're missing).

I agree that using the ** willSwipeCardAway** or didSwipeCardAway functions will ensure that the card is only being dragged once, although the issue is that I still want the card to be available after swiping left. This way, once the user is done viewing the new view controller that swiping a card produces, he/she has the ability to go back and view the card they swiped on.

Makes sense, do you have any suggestions? Perhaps we could add an extra didDragCard delegate method that returns a dragOffset indicating how far the card is relative to the center? Of course you'd still need to disable swiping after reaching the offset you need..

Makes sense, do you have any suggestions? Perhaps we could add an extra didDragCard delegate method that returns a dragOffset indicating how far the card is relative to the center? Of course you'd still need to disable swiping after reaching the offset you need..

Hmm...I think that is a good idea. Although, when would I as a developer know where to re-enable swiping once the card is visible again?

You'd want to re-enable it when you dismiss the new ViewController right? Plenty of ways of doing that, you could use a delegate callback or notification (observer) to re-enable it once you dismiss the viewcontroller for example.