1

I want somehow when my cellItem is selected to increase a little in size , different than the others. Actually the same size but the one selected to be one step higher than the others. This have to do with the layout? Is there any kind of method? Or storyboard?

I've attached some photos to see what I ve done till now and how it suppose to be. How it shoud be

How it should be

enter image description here

How it is right now.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253

4 Answers4

1
  1. Create outlet for top and bottom constraint of that greenBorderd view.

  2. Suppose we have set them value as 10. On click of collectionView item change its topConstraint value to 0 and bottomConstraint value to 20.

  3. Do this in collectionViewDidSelect method and in colletionView didDeselectItemAt method set them to original values

  4. Write the same code in cellForItem method of UICollectionView.

Please see code below

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TableCollectionViewCell", for: indexPath) as! TableCollectionViewCell

            if cell.isSelected == true{
                    self.changeSelectedCellFrame(cell)
            }
            else
            {
                    self.changeDeselectedCellFrame(cell)
            }
            return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

            let cell = collectionView.cellForItem(at: indexPath) as! TableCollectionViewCell
            self.changeSelectedCellFrame(cell)

    }



    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

           if  let cell = collectionView.cellForItem(at: indexPath) as?TableCollectionViewCell
           {
                    self.changeDeselectedCellFrame(cell)
           }
    }


    func changeSelectedCellFrame(_ cell:TableCollectionViewCell) {
            cell.layoutIfNeeded()
            cell.viewTopConstraint.constant = 0
            cell.viewBottomConstraint.constant = 20
            cell.layoutIfNeeded()
    }


    func changeDeselectedCellFrame(_ cell:TableCollectionViewCell) {
            cell.layoutIfNeeded()
            cell.viewTopConstraint.constant = 10
            cell.viewBottomConstraint.constant = 10
            cell.layoutIfNeeded()
    }

It will show you effect same as you expect.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Hemant Sabale
  • 317
  • 2
  • 13
  • .viewTopConstraint . -> not a member – Fagadariu Marcel Dec 21 '17 at 20:34
  • viewTopConstraint is an IBOutlet for view TopConstraint. Make its value to 10 and set same value for bottomConstraint. This 2 constraint will show white space on top and at the bottom of UiView. Just change that constraint it will give you o/p as expected. – Hemant Sabale Dec 22 '17 at 04:31
0

Please try this code

[UIView transitionWithView:collectionView 
                  duration:.5 
                   options:UIViewAnimationOptionTransitionCurlUp 
                animations:^{

    //Set Size of Cell
    cell.frame = CGRectMake(3, 14, 100, 100);

} completion:^(BOOL finished) {


}];
Rinto Andrews
  • 60
  • 1
  • 9
  • yes UIView.transition(with: collectionView, duration: 0.5, options: .transitionCurlUp, animations: {() -> Void in cell.frame = CGRect(x: 3, y: 14, width: 100, height: 100) }, completion: {(_ finished: Bool) -> Void in }) – Rinto Andrews Dec 21 '17 at 08:58
0

If you want to zoom a little bit you can use:

 override func viewDidLoad()
    {
        super.viewDidLoad()
        collectionView.allowsSelection = true
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        if cell.isSelected == true
        {
            cell.transform = CGAffineTransform(scaleX: 1.2, y: 2)
        }
        else
        {
            cell.transform = .identity
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.isSelected = true
        UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 5, initialSpringVelocity: 0, options: [], animations:
        {
            cell?.transform = CGAffineTransform(scaleX: 1.2, y: 2)
        })
    }

There is another alternative were you can add a subview to the cell where the bottom constraint of it is equal to 16 (for example), and on selection you can change the border color of that view.

Ayman Ibrahim
  • 1,359
  • 15
  • 24
0
  1. Create new flowlayout by overriding UICollectionViewFlowLayout
  2. Assign it to the collectionview
  3. Store selected item index on custom flowlayout
  4. When layoutAttributesForItemAtIndexPath: modify the frame size of the item

Hint: Cache calculated layoutattributes for indexpaths because they are going to be asked from the collectionview many times.