hectormatos2011 / iOSContextualMenu

An easy-to-plug-in Contextual Menu for iOS inspired by Pinterest.
MIT License
85 stars 27 forks source link

Suggestion for improvement / Use with collectionViews cells #12

Closed salvasp closed 4 years ago

salvasp commented 6 years ago

I'm using it for collection views cells and it seems that didSelectItemAt of the collection view is not called because the menu gesture recognizers overrides the touches. So to get the cell indexPath where the menu is shown I have modified:

//ContextualMenu+Extension

-    @objc(contextualMenu:didSelectItemAtIndex:)
-    optional func contextualMenu(_ menu: ContextualMenu, didSelectItemAt index: Int)
+    @objc(contextualMenu:didSelectItemAtIndex:sourceAtPoint:)
+    optional func contextualMenu(_ menu: ContextualMenu, didSelectItemAt index: Int, sourceAt point: CGPoint)

and

//ContextualMenu / didSelect(menuItemView: ContextualMenuItemView)

//      delegate?.contextualMenu?(self, didSelectItemAt: menuItem.index)
        delegate?.contextualMenu?(self, didSelectItemAt: menuItem.index, sourceAt: startingLocation)

Using it this way

func contextualMenu(_ menu: ContextualMenu, didSelectItemAt index: Int, sourceAt point: CGPoint) {
let point2 = UIApplication.shared.keyWindow?.convert(point, to: collectionView)
let indexPath = collectionView.indexPathForItem(at: point2)
let cell = collectionView.cellForItem(at: indexPath)
}

Is it acceptable or is there a better way to use it with collectionViews?

salvasp commented 6 years ago

should be modified also the other metods accordingly like

//        delegate?.contextualMenu?(self, didSelectItemAt: menuItem.index)
        delegate?.contextualMenu?(self, didSelectItemAt: menuItem.index, sourceAt: startingLocation)
///
        guard delegate?.contextualMenuShouldActivate?(self, sourceAt: startingLocation) != false && !contextualMenuItems.isEmpty && shadowView.alpha == 0.0 && window != nil else { return }

In this way you can calculate whether a cell or the background has been touched in the CollectionView and return true / false

salvasp commented 6 years ago

OR you can change the menu.startingLocation to be @objc public var