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?