你如何设置UICollectionView动画的持续时间? [英] How do you set the duration for UICollectionView Animations?

查看:158
本文介绍了你如何设置UICollectionView动画的持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义流布局,它调整单元格在从CollectionView中插入和删除以下两个函数时的属性,但我无法弄清楚如何调整默认动画持续时间。 / p>

   - (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {
UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath] ;

//分配新的布局属性
attributes.transform3D = CATransform3DMakeScale(0.5,0.5,0.5);
attributes.alpha = 0;

返回属性;

}

 <$ (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {

UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];

//分配新的布局属性
attributes.transform3D = CATransform3DMakeScale(0.5,0.5,0.5);
attributes.alpha = 0;

返回属性;

}

解决方案为了解决通过gavrix
回答中提出的破解问题,您可以继承UICollectionViewLayoutAttributes使用新属性 CABasicAnimation * transformAnimation ,比创建具有合适持续时间的自定义转换并将其分配给 initialLayoutAttributesForAppearingItemAtIndexPath 中的属性,然后在UICollectionViewCell中根据需要应用这些属性:

  @interface AnimationCollectionViewLayoutAttributes:UICollectionViewLayoutAttributes 
@property(nonatomic,strong)CABasicAnimation * transformAnimation;
@end

@implementation AnimationCollectionViewLayoutAttributes
- (id)copyWithZone:(NSZone *)zone
{
AnimationCollectionViewLayoutAttributes * attributes = [super copyWithZone:zone ]。
attributes.transformAnimation = _transformAnimation;
返回属性;


- (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

在布局类中

   - (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;
返回属性;
}

然后在UICollectionViewCell中应用这些属性

   - (void)applyLayoutAttributes:(AnimationCollectionViewLayoutAttributes *)layoutAttributes 
{
[[self layer] addAnimation:layoutAttributes.transformAnimation forKey:@transform ]。
}


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;

}

解决方案

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;
}

then in UICollectionViewCell apply the attributes

- (void) applyLayoutAttributes:(AnimationCollectionViewLayoutAttributes *)layoutAttributes
{
    [[self layer] addAnimation:layoutAttributes.transformAnimation forKey:@"transform"];
}

这篇关于你如何设置UICollectionView动画的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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