简体   繁体   English

UICollectionView动画可重复使用的单元格?

[英]UICollectionView animating reusable cells?

I've got a UICollectionView that is formatted as a single, horizontally scrollable row across the screen. 我有一个UICollectionView ,其格式设置为屏幕上单个可水平滚动的行。 It represents the next-up entries in a queue, so whenever items are added and deleted, it always happens at Index 0 (the far left of the View) and then the whole thing moves forward or back by one space. 它表示队列中的下一个条目,因此无论何时添加和删除项目,它始终发生在索引0(视图的最左侧),然后整个对象向前或向后移动一个空格。 It's super-simple in the code, just: 它在代码中非常简单,只需:

[_collectionviewNextUp insertItemsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]];

and

[_collectionviewNextUp deleteItemsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]];

Everything is working properly, except the animations for insertItemsAtIndexPaths: and deleteItemsAtIndexPaths: , specifically when the CollectionView items extend beyond the edge of the screen. 除了insertItemsAtIndexPaths:deleteItemsAtIndexPaths:的动画外,其他所有东西都正常运行,特别是当CollectionView项超出屏幕边缘时。 As long as there are only 6 items in the queue (the number that can be seen at once), it animates perfectly; 只要队列中只有6个项目(可以立即看到的数字),它就可以完美制作动画。 but 7 items or more and the animation gets weird. 但超过7个项目,动画变得怪异。 Delete an item (from the left) and it also animates one being added to the right; 删除一项(从左侧开始),它还会为右侧添加的一项添加动画效果; add an item (on the left) and it also animates one being deleted from the right. 添加一个项目(在左侧),同时还为从右侧删除的项目设置动画效果。 Even if the queue is 50 items long, it always animates item #6 (the last visible one onscreen) disappearing and reappearing. 即使队列有50个项目,它也总是使第6个项目(屏幕上最后一个可见的项目)动画消失并重新出现。 There are no errors or warnings or anything, and I'm left with the correct number of items in the View after the animation is over, it just animates a cell being added/deleted at the edge of the screen. 没有错误,警告或其他任何内容,动画结束后,我在“视图”中剩下的项目数正确,它只是在屏幕边缘设置了要添加/删除的单元格的动画。

My theory is that the reusable cells are being animated. 我的理论是,可重用的单元正在被动画化。 Does that sound like it could be a thing? 听起来像是一回事吗? Scrolling left and right works perfectly, but inserting and deleting seems to only animate in pairs, and they always "fly in" and "fly out" in the same direction, as though one is taking the other's place. 向左和向右滚动的效果很好,但是插入和删除似乎只能成对进行动画处理,并且它们总是以相同的方向“飞进”和“飞出”,就好像一个人在取代另一个人的位置一样。 It looks (to my inexperienced eye) like the cell on the end is being flown over and added to the front, or the one on the front is being moved over to the end. (对我而言,经验不足的人)看起来像是将末端的单元格飞过并添加到前端,或者将前端的单元格移到末端。

I've tried to do research on this but all I can find are animation problems that throw errors and exceptions, which mine doesn't do. 我曾尝试对此进行研究,但我发现的都是动画问题,这些问题会引发错误和异常,而我的却无法做到。 Has anyone else experienced this? 其他人有没有经历过? And can anyone offer a fix, wherein the Index 0 item gets animated and the rest just smoothly slide onto and off of the screen? 谁能提供一种解决方法,使索引0的项目变为动画,其余的平滑地滑入或滑出屏幕?

I assume you're using flow layout. 我假设您正在使用流布局。 Flow layout is very buggy animating items across the layoutAttributesForElementsInRect: boundary (ie items moving in and out of view). 流布局是非常有问题的动画,可以跨layoutAttributesForElementsInRect:边界(即,项目移入和移出视图)对项目进行动画处理。 In my experience, you can fix some of these issues by overriding the 'initialLayoutAttributesForAppearingItem' & finalLayoutAttributesForDisappearingItemAtIndexPath: methods and correcting the frames when the super class returns incorrect values. 以我的经验,您可以通过重写'initialLayoutAttributesForAppearingItem'和finalLayoutAttributesForDisappearingItemAtIndexPath:方法并在超类返回错误值时更正帧来解决其中一些问题。 Here is a successful answer to a very similar issue . 这是一个非常相似的问题的成功答案。

I open sourced a simple uniform grid layout VCollectionViewGridLayout that greatly improves animations over what UICollectionViewFlowLayout provides. 我开源了一个简单的统一网格布局VCollectionViewGridLayout ,它大大改善了UICollectionViewFlowLayout提供的动画。 However, it currently only does vertical scrolling. 但是,当前仅进行垂直滚动。 But if you went in there and transposed all of the horizontal and vertical calculations, I'm sure it would solve your issue. 但是,如果您进入那里并转置了所有水平和垂直计算,我相信它将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM