Adding UIPointerInteraction to UICollectionViewCell

Hi All,


Can anybody help me out with how to implement UIPointerInteraction (Which available with iPadOS 13.4) for UICollectionView Cell?


I have referred this link: developer.apple.com/documentation/uikit/pointer_interactions


I want to highlight or lift UICollectionViewCell when I mouse over it on iPad...


Thanks Again...

Answered by elitedeveloper in 413343022

I was able to solve it myself...


My code looks something like this now...


customPointerInteraction(on: cell.self, pointerInteractionDelegate: self)

What have you written so far ?

What does not work ?


If not yet done, may have a look at this tutorial:

h ttps://pspdfkit.com/blog/2020/supporting-pointer-interactions/

I implemented the below code from Apple Documentation:


if #available(iOS 13.4, *) {
       
        func pointerInteraction(_ interaction: UIPointerInteraction, styleFor region: UIPointerRegion) -> UIPointerStyle? {
            var pointerStyle: UIPointerStyle? = nil

            if let interactionView = interaction.view {
                let targetedPreview = UITargetedPreview(view: interactionView)
                pointerStyle = UIPointerStyle(effect: UIPointerEffect.lift(targetedPreview))
            }
           
            return pointerStyle
        }
}

But it is gets applied on buttons on Navigation bar but not on UICollectionViewCell...


I even tried this link: owncloud.org/news/of-mice-and-mobile-devices-implementing-mouse-interaction-in-ipados-13-4/


But I don't know how to apply the same hover effect on UICollectionViewCell...!!!

Did you check if the function is fired when you hover over the cell ?


Instrument the code:


        func pointerInteraction(_ interaction: UIPointerInteraction, styleFor region: UIPointerRegion) -> UIPointerStyle? {
            var pointerStyle: UIPointerStyle? = nil
            print("pointerInteraction view", interaction.view)
            if let interactionView = interaction.view {
                let targetedPreview = UITargetedPreview(view: interactionView)
                pointerStyle = UIPointerStyle(effect: UIPointerEffect.lift(targetedPreview))
            }
          
            return pointerStyle
        }

Do you get a print ?

What do you get ?


Did you declare conformance to UIPointerInteractionDelegate protocol ?


Did you addInteraction when you create the cells ?

In tableView(…. cellForRowAt …)

you should have something like describe in the link you provided:

  customPointerInteraction(on: cell.view, pointerInteractionDelegate: self)

Hi,


I added below print function but it doesn't print anything on mouse hover over cell...

print("pointerInteraction view", interaction.view)


I added the below code to cellForItemAt for CollectionViewCell, It gives me an error: Value of type 'CollectionViewCell' has no member 'view'

customPointerInteraction(on: cell.view, pointerInteractionDelegate: self)
Accepted Answer

I was able to solve it myself...


My code looks something like this now...


customPointerInteraction(on: cell.self, pointerInteractionDelegate: self)

Thanks for feedback.


As I thought, you had to apply customPointerInteraction(on: the right object.

Adding UIPointerInteraction to UICollectionViewCell
 
 
Q