Daniel
Daniel

Reputation: 7724

tvos elevate collection view cell on focus

I want to increase the size of an UICollectionViewCell focused inside the collection view.

How can I increase the size of an UICollectionViewCell when it's focused? I couldn't do it with CGAffine, I tried this:

cellChosen.transform = CGAffineTransform(scaleX: 2, y: 2)

but it didn't work. Did I do it wrongly?

There is also an effect on the UIImageView called adjustImageWhenAncestorFocused that is quite cool. Is it possible to use it on the cell ?

IMPORTANT: I'm using swift 3 and the cell has an UILabel and a UIImageView.

Upvotes: 3

Views: 2579

Answers (2)

Alizain Prasla
Alizain Prasla

Reputation: 754

Add collection view delegate that avoid focus collectionview

func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}

override UIFocusEnvironment in UICollectionViewCell

override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

    coordinator.addCoordinatedAnimations({

        if self.focused 
        {
          // Apply focused appearence
        } 
        else 
        {
          // Apply normal appearance
        }


    }, completion: nil)
}

For transition. add these lines on UICollectionViewCell

override func awakeFromNib() {
    super.awakeFromNib()

    imageView.adjustsImageWhenAncestorFocused = true
    imageView.clipsToBounds = false
    label.alpha = 0.0
}

Upvotes: 3

sschunara
sschunara

Reputation: 2285

You need to use transform in didUpdateFocusInContext. For example

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
    if (self.focused)
    {
        // Apply focused appearence,
        // e.g scale both of them using transform or apply background color 
    }
    else
    {
       // Apply normal appearance
    }
}

Upvotes: 4

Related Questions