Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the duration for UICollectionView Animations?

I have a custom flow layout which is adjusting the attributes for cells when they are being inserted and deleted from the CollectionView with the following two functions, but I'm unable to figure out how you would adjust the default animation duration.

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {     UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];      // Assign the new layout attributes     attributes.transform3D = CATransform3DMakeScale(0.5, 0.5, 0.5);     attributes.alpha = 0;      return attributes; }  - (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {      UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];      // Assign the new layout attributes     attributes.transform3D = CATransform3DMakeScale(0.5, 0.5, 0.5);     attributes.alpha = 0;      return attributes; } 
like image 448
James Parker Avatar asked Oct 16 '12 20:10

James Parker


People also ask

What is the default animation time?

Defines how long the animation lasts. default animation-duration: 0s; The default value is zero seconds: the animation will simply not play.

How does Uicollectionview work?

The collection view presents items onscreen using a cell, which is an instance of the UICollectionViewCell class that your data source configures and provides. In addition to its cells, a collection view can present data using other types of views.

What is Uicollectionview flow layout?

Overview. A flow layout is a type of collection view layout. Items in the collection view flow from one row or column (depending on the scrolling direction) to the next, with each row containing as many cells as will fit. Cells can be the same sizes or different sizes.


2 Answers

To solve problem without hack that was proposed in the answer by gavrix you could subclass UICollectionViewLayoutAttributes with new property CABasicAnimation *transformAnimation, than create custom transformation with a suitable duration and assign it to attributes in initialLayoutAttributesForAppearingItemAtIndexPath, then in UICollectionViewCell apply the attributes as needed:

@interface AnimationCollectionViewLayoutAttributes : UICollectionViewLayoutAttributes @property (nonatomic, strong)  CABasicAnimation *transformAnimation; @end  @implementation AnimationCollectionViewLayoutAttributes - (id)copyWithZone:(NSZone *)zone {     AnimationCollectionViewLayoutAttributes *attributes = [super copyWithZone:zone];     attributes.transformAnimation = _transformAnimation;     return attributes; }  - (BOOL)isEqual:(id)other {     if (other == self) {         return YES;     }     if (!other || ![[other class] isEqual:[self class]]) {         return NO;     }     if ([(( AnimationCollectionViewLayoutAttributes *) other) transformAnimation] != [self transformAnimation]) {         return NO;     }      return YES; } @end 

In Layout class

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {     AnimationCollectionViewLayoutAttributes* attributes = (AnimationCollectionViewLayoutAttributes* )[super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];      CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];     transformAnimation.duration = 1.0f;     CGFloat height = [self collectionViewContentSize].height;      transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 2*height, height)];     transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, attributes.bounds.origin.y, 0)];     transformAnimation.removedOnCompletion = NO;     transformAnimation.fillMode = kCAFillModeForwards;     attributes.transformAnimation = transformAnimation;     return attributes; }  + (Class)layoutAttributesClass {      return [AnimationCollectionViewLayoutAttributes class];  } 

then in UICollectionViewCell apply the attributes

- (void) applyLayoutAttributes:(AnimationCollectionViewLayoutAttributes *)layoutAttributes {     [[self layer] addAnimation:layoutAttributes.transformAnimation forKey:@"transform"]; } 
like image 107
zyxel Avatar answered Sep 28 '22 05:09

zyxel


change CALayer's speed

@implementation Cell - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) {     self.layer.speed =0.2;//default speed  is 1 } return self; } 
like image 29
rotoava Avatar answered Sep 28 '22 05:09

rotoava