【问题标题】:ViewWillTransitonToSize rotation AnimationViewWillTransitonToSize 旋转动画
【发布时间】:2015-01-05 14:14:15
【问题描述】:

我正在尝试替换 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

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

【问题讨论】:

  • 你有没有上传过代码,我可以试试看有什么问题?
  • 抱歉@rakeshbs,我没有在线代码,但它是使用常规collectionViewFlowLayout 的简单collectionView。唯一的自定义功能是在 ViewWillTransitionToSize 中编码的旋转动画。
  • @rakeshbs 我准备一个样例项目放到网上
  • @rakeshbs 这是一个带有工作 Xcode 示例的 zip:files.cargocollective.com/518889/swift-collectionview.zip

标签: ios swift viewwilltransitiontosize


【解决方案1】:

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

https://github.com/rakeshbs/RotatingCollectionView

【讨论】:

  • 哇,这太棒了! @rakeshbs 看起来很棒!我会尝试一下。非常感谢!
  • 这是一个有趣的挑战 :) 如果你尝试更多,也许你可以加快速度。我很着急
  • 我检查了性能,它肯定比以前慢。但它有效,我真的很喜欢你的方法。我会做一些实验来尝试加快这个过程。再次,非常感谢!真的很欣赏它。 @rakeshbs
  • 仔细查看您的代码后,我发现了一些有趣的事情:您在 -collectionView:cellForItemAtIndexPath 中的单元格中添加了急需的旋转变换,当它使单元格出列时。添加这行代码: myCell.contentView.transform = self.counterRotation 并保留我原来的 viewWillTransitionToSize 实现是解决问题所需要的 :) @rakeshbs
  • 我想我修好了。我一直依靠 collectionView.visibleCells() 数组来为每个单元格的旋转设置动画。我所做的是创建一个新数组,它不仅包含 visibleCells() 的副本,还包含在我们滚动时出列的新单元格。这个新数组就是 ViewWillTransitionToSize 动画块中的内容。当 collectionsView 滚动时,新出列的单元格会弹出到数组中,动态生成动画然后被删除,以防止数组变得太大。这样可以在单元格出现在屏幕上时实现平滑的动画效果。
【解决方案2】:

出于单元重用目的,滚动时将始终调用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
}

【讨论】:

  • 是的。这正是我在对@rakeshbs 提供的示例进行几次实验后得出的结论。无论如何,谢谢你停下来:)
  • 很好,但是在iphone 5中仍然出现重复的复制帧,在iphone 6中,我看不到另一个单元格副本,但可以看到屏幕闪烁1秒,怎么办你修这个?我能想象的是暂时增加根视图的大小,不确定这是否可行我还没有尝试过。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 2011-05-20
  • 2013-05-22
  • 2018-11-25
  • 2014-03-04
  • 1970-01-01
相关资源
最近更新 更多