0

我正在尝试替换 UIcollectionView 上的默认 iOS 设备旋转动画。我在 transitionCoordinator 上使用 viewWillTransitionToSize 和 targetTransform() 来防止默认视图旋转,然后使用变换将每个 visibleCell 旋转到正确的方向。它工作正常,除了:

  1. 可见矩形直接外部边界上的单元格没有旋转。
  2. 我的日志显示 collectionView.visibleCells() 数组给了我它应该做的:可见单元格,但我发现如果我让视图以默认动画旋转,则 visibleCells 数组给我可见单元格加上单元格在附近。
  3. 我一直在尝试访问这些“社区”单元,以便可以旋转它们,但我所有的尝试都失败了。

这是我的 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
                 }
             })}
         })
)

这是一个快速的 gif。说明问题: http: //www.blessinglopes.com/Info

任何帮助将不胜感激!谢谢!

4

2 回答 2

1

我通过实现一个单独的线程解决了这个问题,其中单元格被动画化。您可以在下面的 git 存储库中查看代码。

https://github.com/rakeshbs/RotatingCollectionView

于 2015-01-06T05:32:47.480 回答
0

从单元格重用的目的,collectionView:cellForItemAtIndexPath将始终在您滚动时调用。如此简单,确保所有新出现的单元格都应用正确的新方向。例如,在单元格出列后将此行添加cell.contentView.transform = self.counterRotation到您的行中。collectionView:cellForItemAtIndexPath

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
}
于 2015-03-26T19:06:51.297 回答