-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Functionality similar to scrollToItem(at: indexPath) from UICollectionView #23
Comments
Hi @marcmora Not officially supported yet, but since the Here's how it should work: let indexToScrollTo = 9
verticalCardSwiperView.scrollToItem(at: IndexPath(row: indexToScrollTo, section: 0), at: .top, animated: true) You can access the Hope this helps Kind regards, |
…iper - add ScrollToCard function - move indexesForVisibleCards to VerticalCardSwiper - update documentation
I have added this function to the VerticalCardSwiper in the development branch, will be released with the next version. I'm closing this issue for now, feel free to reopen. |
Hi @JoniVR thanks for the quick reply and for exposing the function! I did try to call the Am I missing something? I'm adding it right after registering the Nib as I want the card number n to be the starting point when the user lands into this view controller. |
I'll look into it when I get some more time, thanks for bringing it to my attention. 🙂 |
Okay, so here's probably what's causing your issue: When you call the It would look like this in your override func viewDidLayoutSubviews() {
cardSwiper.scrollToCard(at: 5, animated: true)
} This should work (I've tried it in the example), however, if it doesn't, feel free to let me know 🙂 I'll update the documentation on this function to indicate that for future users. Have a nice day 🙂 edit: |
Hello, i'm experiencing a problem with scrollToCard function. For some reason I can't use it to go to the previous card. For example, if the card with index 4 is active, cardSwiper.scrollToCard(at: 3, animated: true) does nothing, but I can use it to move to any other card (index 0, 1, 2, 5 and so on...). Any idea on how to solve this? Thanks in advance. |
@Phuzer Ok, so after looking into this a bit further, I think it's a bug that's related to the |
Hello @JoniVR I need the option to programatically go to the previous/next card, and as you're already aware, there is a bug in scrollToCard. Therefore, I implemented two methods in ExampleViewController based on scrollRectToVisible: func goToNext() {
let index = cardSwiper.indexesForVisibleCards.last!
let height = cardSwiper.verticalCardSwiperView.layoutAttributesForItem(at: IndexPath(item: index, section: 0))!.frame.height
var indexOffset = 2
// Top of stack (one card visible)
if (cardSwiper.indexesForVisibleCards.count == 1) {
indexOffset = 1
}
let rect = CGRect(x: 0, y: (height + cardSwiper.cardSpacing) * CGFloat(index - indexOffset), width: cardSwiper.verticalCardSwiperView.frame.width - cardSwiper.sideInset * 2, height: height)
cardSwiper.verticalCardSwiperView.scrollRectToVisible(rect, animated: true)
} func goToPrevious() {
let index = cardSwiper.indexesForVisibleCards.last!
let height = cardSwiper.verticalCardSwiperView.layoutAttributesForItem(at: IndexPath(item: index, section: 0))!.frame.height
let rect = CGRect(x: 0, y: (height + cardSwiper.cardSpacing) * CGFloat(index) - cardSwiper.cardSpacing, width: cardSwiper.verticalCardSwiperView.frame.width - cardSwiper.sideInset * 2, height: height)
cardSwiper.verticalCardSwiperView.scrollRectToVisible(rect, animated: true)
} It's a bit fishy, but it works :) I hope it can inspire you to come up with a better solution in VerticalCardSwiper.swift Thanks for the project ;) Keep up the good work! PS: In our app we start with all the cards loaded on the stack hence the names goToNext()/goToPrevious() and their behaviours. |
@pcentieiro Thanks for posting this! I've also looked into using the |
Ok, so little update, I think I've figured out what the problem is, I commited some updated documentation on
Essentially, what's happening is that when try to scroll to the previous card, the I hope this explains it well enough to understand it. at first I was hoping simply doing something like: if let frame = verticalCardSwiperView.layoutAttributesForItem(at: convertIndexToIndexPath(for: index))?.frame {
verticalCardSwiperView.scrollRectToVisible(frame, animated: true)
} would fix it, but it gives the exact same result. edit: Also, if you have a fix, suggestion or feedback, feel free to let met know 🙂 |
Great explanation @JoniVR , thanks :) The problem lies in the frame generated that is used on scrollToRectToVisible. Here is the one I get when I use index = 0 and I'm at the second card (so only 2 cards in the stack and I want to go back): ▿ origin : (17.673921267470945, 523.8456659619451)
- x : 17.673921267470945
- y : 523.8456659619451
▿ size : (299.65215746505805, 473.0)
- width : 299.65215746505805
- height : 473.0 And here is the frame that I generate with the code that I posted before (which scrolls to the previous card with no problem): ▿ origin : (0.0, 0.0)
- x : 0.0
- y : 0.0
▿ size : (335.0, 473.0)
- width : 335.0
- height : 473.0 As you can see the problem is on the y value. I'll try to see what this is happening, but I think that you'll get there before me :) |
I think I've managed to fix it by using |
Is there a way i could skip straight to whatever card of the stack? So the user can go back and forth starting from that card.
Thanks!
The text was updated successfully, but these errors were encountered: