Как добиться эффекта параллакса с помощью UIPointerInteraction для ячеек представления коллекции - PullRequest
0 голосов
/ 28 апреля 2020

Я пытаюсь добиться эффекта параллакса, похожего на tvOS. Когда пользователь указывает на ячейку (с помощью мыши или трекпада в iPadOS), я хочу, чтобы курсор принял форму ячейки.

Я прочитал и следовал документации Apple по этому вопросу, см. Здесь: https://developer.apple.com/documentation/uikit/pointer_interactions/integrating_pointer_interactions_into_your_ipad_app

Однако я не могу заставить этот эффект работать на ячейках. Он работает на случайных UIViews, которые я добавляю на экран, но никогда на ячейках UICollectionView.

Здесь я вставил код для добавленной функциональности UIPointerInteraction в базовый c UICollectionView Controller. В этом примере ячейка распознает, на что она указывает. Однако это не дает правильного эффекта (курсор не изменяется в размере ячейки.

Примечание: я вызываю cell.addInteraction (UIPointerInteraction (делегат: self)) в метод cellForItemAt в 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
    }
}

...