如何使用UIPointerInteraction来实现视图集的视差效果

问题描述 投票:0回答:1

我正在尝试实现与tvOS类似的视差效果。当用户指向某个单元格时(通过iPadOS中的鼠标或触控板),我希望光标采用该单元格的形式。

我已阅读并遵循了Apple的文档,在这里看到:https://developer.apple.com/documentation/uikit/pointer_interactions/integrating_pointer_interactions_into_your_ipad_app

但是,我无法在细胞上获得这种效果。它适用于我添加到屏幕上的随机UIView,但不适用于UICollectionView单元。

这里,我将用于添加的UIPointerInteraction功能的代码粘贴到基本的UICollectionView控制器中。在此示例中,单元格确实意识到其被指向。但是,它不能产生正确的效果(光标不会变形为单元格的大小。

注意:我在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
    }
}

我正在尝试实现与tvOS类似的视差效果。当用户指向某个单元格时(通过iPadOS中的鼠标或触控板),我希望光标采用该单元格的形式。我阅读并...

ios swift ipad uicollectionview ipados
1个回答
0
投票

当我向矩形完全被非透明视图填充的集合视图添加指针交互时,我看不到任何视差。

当我向矩形未完全填充的集合视图添加指针交互时(请考虑矩形内部的圆形视图),我会看到视差。

© www.soinside.com 2019 - 2024. All rights reserved.