我想UICollectionView
在我的设备旋转期间重新排列单元格,就像evernote iPads app
在笔记中所做的那样。在默认实现中,只有单元格的淡入和淡出,但我希望单元格在旋转期间四处移动。
实现类似动画的推荐方法是什么?我需要创建自定义UICollectionViewLayout
吗?
我想UICollectionView
在我的设备旋转期间重新排列单元格,就像evernote iPads app
在笔记中所做的那样。在默认实现中,只有单元格的淡入和淡出,但我希望单元格在旋转期间四处移动。
实现类似动画的推荐方法是什么?我需要创建自定义UICollectionViewLayout
吗?
我设法通过继承和覆盖这两种方法来获得所需的旋转效果:UICollectionViewFlowLayout
它们是分别定义插入到集合视图中的项目的开始/最终布局信息的两个控制点。initialLayoutAttributesForAppearingItemAtIndexPath
finalLayoutAttributesForDisappearingItemAtIndexPath
见源代码:
。H:
#import "UICollectionView.h"
@interface UITestCollectionViewFlowLayout : UICollectionViewFlowLayout
@end
米:
#import "UITestCollectionViewFlowLayout.h"
@interface UITestCollectionViewFlowLayout ()
{
BOOL _isRotating;
}
@property (strong, nonatomic) NSIndexPath* lastDissappearingItemIndex;
@end
@implementation UITestCollectionViewFlowLayout
@synthesize lastDissappearingItemIndex = _lastDissappearingItemIndex;
// returns the starting layout information for an item being inserted into the collection view
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
UICollectionViewLayoutAttributes* attributes = (UICollectionViewLayoutAttributes *)[self layoutAttributesForItemAtIndexPath:itemIndexPath];
if (_isRotating) // we want to customize the cells layout only during the rotation event
{
if ([self.lastDissappearingItemIndex isEqual:itemIndexPath])
return nil; // do not animate appearing cell for the one that just dissapear
else
{
attributes.alpha = 0;
// setting the alpha to the new cells that didn't match the ones dissapearing is not enough to not see them so we offset them
attributes.center = CGPointMake(attributes.center.x, attributes.size.height * 2 + attributes.center.y);
}
}
return attributes;
}
// returns the final layout information for an item that is about to be removed from the collection view
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath*)itemIndexPath
{
UICollectionViewLayoutAttributes* attributes = (UICollectionViewLayoutAttributes *)[self layoutAttributesForItemAtIndexPath:itemIndexPath];
if (_isRotating)
{
attributes.alpha = 1.0;
self.lastDissappearingItemIndex = itemIndexPath;
}
return attributes;
}
- (void) viewController:(UIViewController *)viewController didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
_isRotating = NO;
}
- (void) viewController:(UIViewController *)viewController willRotateToInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration
{
_isRotating = YES;
}
请注意,我使用的是从创建此流程布局的视图控制器设置的标志,以了解我们何时实际旋转;原因是我希望这种效果仅在旋转期间发生。
我有兴趣听到有关此代码的反馈/改进。