0

So I am using a function that a user can do a long press on a UICollectionView (note: I have multiple collection views on my screen). This triggers an action, but when I try to pass the collection view from my longPressFolder function to the handleLongPress function, it doesn't work.

    override func viewDidLoad() {
        super.viewDidLoad()       

        // add long press to collection View
        longPressFolder(newestFoldersCollectionView)
        longPressFolder(topFoldersCollectionView)
}

   func longPressFolder(collectionview: UICollectionView) {
        // Long press
        let lpgr : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(FolderOverviewController.handleLongPress(_:)))
        lpgr.minimumPressDuration = 0.4
        lpgr.delegate = self
        lpgr.delaysTouchesBegan = true
        collectionview.addGestureRecognizer(lpgr)
    }

This is the part where the code doesn't work. It says that my collection view is unresolved, but I can't seem to find a way to pass the collection view from my one function to the other.

// long press
func handleLongPress(gestureRecognizer : UILongPressGestureRecognizer){
    if (gestureRecognizer.state != UIGestureRecognizerState.Ended){
        return
    }

    let p = gestureRecognizer.locationInView(collectionview)

    if let indexPath: NSIndexPath = (collectionview.indexPathForItemAtPoint(p))!{
        //do whatever you need to do
        ...               
        }
        collectionview.reloadData()
    }        
}
SoundShock
  • 485
  • 1
  • 3
  • 24
  • You're supposed to pass a view to `locationInView()`. Where's it getting the `collectionview` variable in your `handleLongPress` function? – shim Jul 13 '16 at 14:42
  • @shim it isn't at the moment, the issue is that I can't seem to pass my collectionView to the handleLongPress function – SoundShock Jul 13 '16 at 14:46
  • You can get a reference to a gesture recognizer's view if you use `.view` on the recognizer. So try `let collectionview = gestureRecognizer.view as! UICollectionView` – shim Jul 13 '16 at 14:46
  • Hmmm, if I try this, and press the second cell in my second collectionview, it selects the second cell in my first collectionview.. but at least it's progress :) I'll update the code, so you can check if I'm doing anything incorrect. – SoundShock Jul 13 '16 at 14:52
  • @shim please make an answer of your solution, i'll mark it as correct. Was being stupid with the selection of the collectionview ^^ – SoundShock Jul 13 '16 at 15:05
  • OK, but now does your edited code in your question work properly? Might be confusing for anyone else reading this question. – shim Jul 13 '16 at 19:44
  • works properly with your line of code added to it. Deleted it in my question, since it might indeed confuse people if I let it in there. – SoundShock Jul 13 '16 at 20:29

2 Answers2

1

replace

if let indexPath: NSIndexPath = (collectionview.indexPathForItemAtPoint(p))!{

with

if let indexPath: NSIndexPath = ((gestureRecognizer.view as! UICollectionView).indexPathForItemAtPoint(p))!{
Igor
  • 12,165
  • 4
  • 57
  • 73
  • didn't fix my issue. Updated my code to a newer version, which works, but only for the first collectionview. Even if I press on the second one. – SoundShock Jul 13 '16 at 14:58
  • do you have `collectionview` variable declared outside `longPressFolder` or `handleLongPress`? – Igor Jul 13 '16 at 15:04
  • and your collectionViews didn't overlay one another? – Igor Jul 13 '16 at 15:08
  • try also add gesture recognizer only to second collection view and look, does it work – Igor Jul 13 '16 at 15:09
1

You can get a reference to a gesture recognizer's view if you use .view on the recognizer. So try:

let collectionview = gestureRecognizer.view as! UICollectionView
shim
  • 9,289
  • 12
  • 69
  • 108