iPad keyboard navigation: where to initialize UIFocusHaloEffect

WWDC21 Session Focus on iPad keyboard navigation says that we can use UIFocusHaloEffect to change the appearance of focused items.

On iPadOS, we can use this effect by assigning a UIFocusHaloEffect to the focusEffect property like so:

self.focusEffect = UIFocusHaloEffect()

What wasn't very clear is where should we put this code when working with UICollectionViewCell. I am doing it in the collectionView(_:canFocusItemAt:) method:

func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
    guard #available(iOS 15.0, *) else { return false }
    guard let cell = collectionView.cellForItem(at: indexPath) as? FeedCollectionViewCell else { return false }
    if cell.focusEffect == nil {
        cell.focusEffect = UIFocusHaloEffect(roundedRect: cell.artworkImageView.frame,
                                             cornerRadius: cell.cornerRadius,
                                             curve: .continuous)
    }
    return true
}

Is this the best way to implement it?

I believe the best place to implement it is in the init of the cell class. Example:

class GenericCell: UICollectionViewCell {

    static let reuseIdentifier = "my-cell"
    override var canBecomeFocused: Bool { true }

    override init(frame: CGRect) {
        super.init(frame: frame)
        focusEffect = UIFocusHaloEffect()
        ...
    }
    ...
}
iPad keyboard navigation: where to initialize UIFocusHaloEffect
 
 
Q