[英]How to achieve the parallax effect using UIPointerInteraction for Collection View Cells
I am trying to achieve a parallax effect that is similar to that of tvOS.我正在尝试实现类似于 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.
当用户指向一个单元格时(通过 iPadOS 中的鼠标或触控板),我希望 cursor 采用单元格的形式。
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我阅读并遵循了 Apple 的相关文档,请参见此处: 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.
它适用于我添加到屏幕的随机 UIView,但从不适用于 UICollectionView 单元格。
Here, I pasted code for the added UIPointerInteraction functionality to a bare basic UICollectionView Controller.在这里,我将添加 UIPointerInteraction 功能的代码粘贴到了一个基本的 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.
但是,它不会产生正确的效果(cursor 不会变形为单元格的大小。
Note: I call cell.addInteraction(UIPointerInteraction(delegate: self)) in the cellForItemAt method in the collectionView.注意:我在collectionView的cellForItemAt方法中调用了cell.addInteraction(UIPointerInteraction(delegate: self)) 。
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
}
}
When I add a pointer interaction to a collection view cell whose rectangle is entirely filled with non-transparent views I do not see any parallax.当我将指针交互添加到其矩形完全填充有非透明视图的集合视图单元格时,我看不到任何视差。
When I add a pointer interaction to a collection view cell whose rectangle is not entirely filled (think a circle view inside a rectangle) I see parallax.当我将指针交互添加到其矩形未完全填充的集合视图单元格时(想想矩形内的圆形视图),我看到了视差。
The code is the exact same for both collection views/cells for pointer interaction too (much like your code above).用于指针交互的集合视图/单元格的代码也完全相同(很像上面的代码)。 My assumption is that Apple only applies parallax if there is (sufficient?) transparency within the cell.
我的假设是,如果单元格内有(足够的?)透明度,Apple 只会应用视差。
Here is my current code:这是我当前的代码:
func pointerInteraction(_ interaction: UIPointerInteraction, regionFor request: UIPointerRegionRequest, defaultRegion: UIPointerRegion) -> UIPointerRegion? {
return defaultRegion //I don't think you even need this method unless you want to change the region...
}
func pointerInteraction(_ interaction: UIPointerInteraction, styleFor region: UIPointerRegion) -> UIPointerStyle? {
return UIPointerStyle(effect: .lift(.init(view: self)))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.