Reputation: 570
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:
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!
Upvotes: 0
Views: 2609
Reputation: 490
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
}
Upvotes: 0
Reputation: 24572
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
Upvotes: 1