Skip to content
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

Intercept PagingMenuItem click and scroll to index manually #716

Open
tarun-personatech opened this issue May 30, 2024 · 0 comments
Open

Comments

@tarun-personatech
Copy link

tarun-personatech commented May 30, 2024

This is not an issue but more of asking for help.

I need help intercepting the click on a PagingMenuItem before the associated ViewController is scrolled on the screen. By default, this click is detected from the PagingViewController's collectionView(_:didSelectItemAt:) data source method. However in my case, once the click is detected, I want to run some logic that can do one of the following:

  • If the tapped item cannot be selected yet based on some business logic, I want to show an alert instead of scrolling the tapped viewController on the screen.
  • If the tapped item changes the appearance of other menu items, I want to reload the menu and then scroll the tapped viewController on the screen.

I tried to override the collectionView(_:didSelectItemAt:) method in SelfSizingViewController from the example app like this:

override public func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    DispatchQueue.main.async {
      // Run some business logic here...
      self.reloadMenu()
      self.select(index: indexPath.item, animated: true)
    }
  }

I see a couple of issues here: (on iPhone 15 Pro Max simulator)

  1. The indexPath.item doesn't have the value that you expect so the select method selects the wrong menu item. For example, if you select Once Upon a Time in the West the indexPath.item you get is 8 where it should have been 10, so Psycho gets selected. Interesting here is that Once Upon a Time in the West can be found at index 8 in visibleItems so instead of selecting by index if I do the below, the correct menu item is selected. But I wonder why the indexPath is wrong?

    let pagingItem = self.visibleItems.pagingItem(for: indexPath)
    self.select(pagingItem: pagingItem, animated: true)

  2. When the menu is reloaded and then the selection is made, the selection animation is messed up sometimes and the above issue remains.

I have attached a video of the same.
https://github.com/rechsteiner/Parchment/assets/53808661/b581e102-f500-4f29-991b-2c5023c2b313

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant