I am trying to achieve a parallax effect that is similar to that of tvOS. When a user points to a cell (via a mouse or trackpad in iPadOS), I want the cursor to take the form of the cell.
I read and followed Apple's documentation on this, see here: https://developer.apple.com/documentation/uikit/pointer_interactions/integrating_pointer_interactions_into_your_ipad_app
However, I cannot get this effect to work on cells. It works on random UIViews that I add to the screen, but never on UICollectionView cells.
Here, I pasted code for the added UIPointerInteraction functionality to a bare basic UICollectionView Controller. In this example, the cell does recognize its being pointed at. However, it does not produce the correct effect (the cursor does not morph into the size of the cell.
Note: I call cell.addInteraction(UIPointerInteraction(delegate: self)) in the cellForItemAt method in the collectionView.
extension CollectionViewController: UIPointerInteractionDelegate {
func pointerInteraction(_ interaction: UIPointerInteraction, regionFor request: UIPointerRegionRequest, defaultRegion: UIPointerRegion) -> UIPointerRegion? {
var pointerRegion: UIPointerRegion? = nil
if let cell = interaction.view as? UICollectionViewCell {
//pointer has entered one of the collection view cells
pointerRegion = UIPointerRegion(rect: cell.bounds)
}
return pointerRegion
}
func pointerInteraction(_ interaction: UIPointerInteraction, styleFor region: UIPointerRegion) -> UIPointerStyle? {
var pointerStyle: UIPointerStyle? = nil
if let cell = interaction.view as? UICollectionViewCell {
let parameters = UIPreviewParameters()
parameters.visiblePath = UIBezierPath(rect: cell.bounds)
let targetedPreview = UITargetedPreview(view: cell, parameters: parameters)
let pointerEffect = UIPointerEffect.lift(targetedPreview)
// Shape the pointer to match the inner path of this view.
let pointerShape = UIPointerShape.path(UIBezierPath(rect: cell.bounds))
pointerStyle = UIPointerStyle(effect: pointerEffect, shape: pointerShape)
}
return pointerStyle
}
}