首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用UIPointerInteraction实现集合视图单元格的视差效应

如何利用UIPointerInteraction实现集合视图单元格的视差效应
EN

Stack Overflow用户
提问于 2020-04-28 03:19:23
回答 1查看 559关注 0票数 0

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

我阅读并跟踪了苹果在这方面的文档,参见:https://developer.apple.com/documentation/uikit/pointer_interactions/integrating_pointer_interactions_into_your_ipad_app

然而,我不能让这个效果在细胞上起作用。它在我添加到屏幕上的随机UIViews上工作,但从不在UICollectionView单元上工作。

在这里,我将添加的UIPointerInteraction功能的代码粘贴到一个基本的UICollectionView控制器上。在本例中,单元格确实识别被指向的对象。然而,它并没有产生正确的效果(光标不会改变成单元格的大小。

注意:我在cell.addInteraction(UIPointerInteraction(delegate:( cellForItemAt )方法中调用collectionView中的(Self))。

代码语言:javascript
复制
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
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-06-16 14:59:25

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

当我将指针交互添加到矩形未完全填充的集合视图单元格(假设矩形内有圆圈视图)时,我会看到视差。

对于用于指针交互的集合视图/单元格,代码也完全相同(很像上面的代码)。我的假设是,苹果只有在有足够的视差的情况下才应用视差。牢房内的透明度。

以下是我的当前代码:

代码语言:javascript
复制
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)))
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61472056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档