ViewWillTransitonToSize rotation Animation

2.5k views Asked by At

I'm trying to replace the default iOS device rotation animation on my UIcollectionView. I'm using viewWillTransitionToSize and the targetTransform() on the transitionCoordinator to prevent the default view rotation, and then use a transform to rotate each visibleCell into the correct orientation. It works fine, except:

  1. The Cells on the immediate outer bounds of the visible rect are not getting rotated.
  2. My logs show that the collectionView.visibleCells() array is giving me what it is supposed to: the visible cells, but I found that if i let the view rotate with the default animation, the visibleCells array gives me the visible Cells PLUS the cells in the immediate neighbourhood.
  3. I've been trying to access these "neighbourhood" cells so I can rotate them but all my attempts failed with a bang.

Here’s my implementation of ViewWillTransitionTosize :

override func viewWillTransitionToSize( size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator){
       super.viewWillTransitionToSize(size , withTransitionCoordinator: coordinator)

      let transf : CGAffineTransform = coordinator.targetTransform()
      let invertedRotation = CGAffineTransformInvert(transf)
      let currentBounds = view.bounds
     coordinator.animateAlongsideTransition({
     _ in

     self.view.transform = CGAffineTransformConcat(self.view.transform, invertedRotation )
        self.undoRotation =  CGAffineTransformConcat(self.undoRotation, transf)
     self.view.bounds = currentBounds
}, completion: ({ finished in
         if ( finished != nil){

                 UIView.animateWithDuration(0.5,  animations: {
                 for cell  in self.collectionView!.visibleCells(){
                    cell.contentView.transform = self.undoRotation
                 }
             })}
         })
)

Here a quick gif. to ilustrate the problem: http://www.blessinglopes.com/Info

Any Help will be greatly appreciated! Thank you!

2

There are 2 answers

12
rakeshbs On BEST ANSWER

I have solved the problem by implementing a separate thread in which the cell is animated. You can check out the code in the git repository below.

https://github.com/rakeshbs/RotatingCollectionView

2
Solomon On

From cell reuse purpose, collectionView:cellForItemAtIndexPath will always be called when you scroll. So simple make sure all your new appeared cells are applied the correct new orientation. For example add this line cell.contentView.transform = self.counterRotation in your collectionView:cellForItemAtIndexPath after the cell is dequeued.

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell : MyCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CellIdentifier", forIndexPath: indexPath) as MyCollectionViewCell
    cell.imageView.image = UIImage(named: "MyImg")!
    cell.contentView.transform = self.undoRotation
    return cell
}