如何正确地为分页 UICollectionView 的大小变化设置动画 [英] How to correctly animate change of size of a paged UICollectionView

查看:55
本文介绍了如何正确地为分页 UICollectionView 的大小变化设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于 iPad Keynote 的 iPad 应用程序,左边是一个狭窄的概述,右边是我的幻灯片"的分页 UICollectionView.集合视图使用默认 FlowLayout.其中一些幻灯片是标准的 PDF,一些是嵌入的 UIViewController 已缩放(使用 CGAffineTransform)并嵌入到单元格中.我想在屏幕外平滑地设置概览侧边栏的动画并将当前页面单元格缩放到全屏.集合视图应该允许以任何大小进行分页滑动.我正在使用故事板和自动布局.

我认为我需要同时为三件事制作动画:

  • 集合视图约束(到侧边栏)以放大/缩小它
  • 流布局的 sizeForItemAt:
  • 嵌入式视图控制器上的 CGAffineTransform.

我有一些工作(一个可以正确缩放的嵌入式视图控制器幻灯片"),但无法使 collectionView/cell 调整大小舞蹈正常工作.单元格调整大小动画生涩,或以错误的偏移结束,或适用于最左边的单元格但不适用于其他单元格.

我已经尝试了

值得注意的是,仔细检查后,Keynote 在编辑模式下在幻灯片之间滑动时有些作弊,并手动管理屏幕上的下一张幻灯片,因此可能不使用 UICollectionView.

有没有人做过类似的事情,或者对尝试的事情有什么建议?

解决方案

我设法解决了这个问题.此处有一个概念验证 GitHub 存储库.

有几个活动部分:

  • 有两段 UIView 缩放代码.我怀疑这些可以与合适的委托引用相结合,因为嵌套的视图控制器是嵌入的.创建集合视图单元格时,第一部分正确缩放(静态).第二个是边栏大小更改时的动画缩放/平移.

  • 我添加了一个 FlowLayout 子类来消除闪烁,因为在缩放期间集合的布局无效.

  • 顶级 VC 中侧边栏相关的转换使用了书中大部分技巧 - invalidateLayout(), performBatchUpdates(...), layoutIfNeeded() 等,以及将 contentOffset 动画化为正确的值.需要对正确的页面/幻灯片进行一些手动跟踪.

  • 在开始调整大小时偶尔仍会有轻微的闪烁.这可能不是我特定的配色方案的问题,它在深灰色上是黑色的.如果有人可以建议如何追踪或缓解这种情况,则会加分.

I have an iPad app similar to the iPad Keynote with a narrow overview on the left and a paged UICollectionView of my "slides" on the right. The collection view is using the default FlowLayout. Some of these slides are standard PDFs and some are embedded UIViewControllers that have been scaled (with a CGAffineTransform) and embedded in the cell. I'd like to smoothly animate the overview sidebar offscreen and zoom the current page cell to fullscreen. The collection view should allow paged swiping at whatever size. I'm using storyboards and autolayout.

I think I need to simultaneously animate about three things:

  • The collection view constraints (to the sidebar) to enlarge/shrink it
  • The flow layout's sizeForItemAt: value
  • The CGAffineTransform on the embedded view controller.

I have some pieces working (a single embedded View Controller "slide" that scales correctly) but cannot get the collectionView/cell resize dance to work correctly. The cell resize animation is jerky, or ends up with the wrong offset, or works for the leftmost cell but not for other cells.

I've tried most of the suggestions in the answers to this question but with no convincing success. I can't believe it's impossible but at this point I'm considering the smoke and mirrors approach of animating a static slide and hiding it after the animation completes. The attached video - ignoring the glitches - illustrates the kind of effect I'm after:

It's worth noting, on close inspection, that Keynote cheats somewhat when it comes to swiping between slides in edit mode, and manually manages the next slide sliding onscreen, so probably doesn't use a UICollectionView.

Has anyone done anything similar, or have any suggestions for things to try?

解决方案

I managed to solve this. There's a proof-of-concept GitHub repo here.

There are a few moving parts:

  • There are two pieces of UIView scaling code. I suspect these could be combined with suitable delegate references as the nested view controllers are embedded. The first piece scales (statically) correctly when the Collection View cell is created. The second is an animated scale/translate when the sidebar size is changed.

  • I added a FlowLayout subclass to remove flicker as the collection's layout is invalidated during scaling.

  • The sidebar-related transform in the top-level VC uses most of the tricks in the book - invalidateLayout(), performBatchUpdates(...), layoutIfNeeded() etc. as well as animating the contentOffset to the correct value. Some manual tracking of the correct page/slide is required.

  • There's still a slight flicker occasionally at the start of resizing. This may not be an issue with my particular colour-scheme which will be black on dark gray. Bonus points if anyone can suggest how to track this down or alleviate it.

这篇关于如何正确地为分页 UICollectionView 的大小变化设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆