使用pop创建无尽的动画?

时间:2014-09-21 00:02:21

标签: ios animation facebook-pop

使用Coreanimation框架时,我可以设置动画重复。我想设置一个“吸引注意”模式的按钮,这样可以让他成长和缩小一点,以吸引用户的注意力。

我已经通过完成块链接了增长和缩小动画。问题是我是否以及如何从第二个动画的完成块开始第一个动画。

我确实收到了以下有用的警告。这个问题的优雅解决方案是什么?我不喜欢为这样的东西创造计时器。

  

在这个区块中强烈捕获'scaleAnimation'可能会导致   保留周期

- (void)attractAttention:(BOOL)flag{
    _attractAttention = flag;
    float resizeValue = 1.2f;

        // Grow animation
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.fromValue  = [NSValue valueWithCGSize:CGSizeMake(1.0f, 1.0f)];
    scaleAnimation.toValue  = [NSValue valueWithCGSize:CGSizeMake(resizeValue, resizeValue)];
    scaleAnimation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
            // Grow animation done
        POPSpringAnimation *scaleAnimationDown = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
        scaleAnimationDown.fromValue  = [NSValue valueWithCGSize:CGSizeMake(resizeValue, resizeValue)];
        scaleAnimationDown.toValue  = [NSValue valueWithCGSize:CGSizeMake(1.0f, 1.0f)];

        scaleAnimationDown.completionBlock = ^(POPAnimation *anim, BOOL finished) {
                // Shrink animation done
            if (_attractAttention) {
                [self.layer pop_addAnimation:scaleAnimation forKey:@"scaleUpAnimation"];
            }
        };

        [self.layer pop_addAnimation:scaleAnimationDown forKey:@"scaleDownAnimation"];
    };

    [self.layer pop_addAnimation:scaleAnimation forKey:@"scaleUpAnimation"];
}

编辑:

我还尝试创建动画的弱引用。这样可以消除错误,但动画不再起作用了:

__weak typeof(scaleAnimation) weakAnimation = scaleAnimation;

2 个答案:

答案 0 :(得分:5)

您还可以使用属性autoreveres和repeatCount来实现相同的结果,而无需使用块。它降低了复杂性:

POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(0.5, 0.5)];
scaleAnimation.springBounciness = 0.f;
scaleAnimation.autoreverses = YES;
scaleAnimation.repeatCount=HUGE_VALF;
[layer pop_addAnimation:scaleAnimation forKey:@"scale"];

更好的是,如果你检查这个类https://github.com/facebook/pop/blob/master/pop/POPAnimation.h,你会看到有一个repeatForever属性,所以你可以用它替换repeatCount:

scaleAnimation.repeatForever=YES;

答案 1 :(得分:0)

<强>编辑: 不要使用此解决方案。 POP框架已更新为“重复”标志。我第一次遇到这个问题时就没用了。


我通过一个非常简单的解决方法解决了这个问题:

- (void)attractAttention:(BOOL)flag{
    _attractAttention = flag;
    if (_attractAttention){
        [self animatePop];
    }
}


- (void)animatePop{
    float resizeValue = 1.2f;

        // Grow animation
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];

    scaleAnimation.fromValue  = [NSValue valueWithCGSize:CGSizeMake(1.0f, 1.0f)];
    scaleAnimation.toValue  = [NSValue valueWithCGSize:CGSizeMake(resizeValue, resizeValue)];
    scaleAnimation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
            // Grow animation done
        POPSpringAnimation *scaleAnimationDown = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
        scaleAnimationDown.fromValue  = [NSValue valueWithCGSize:CGSizeMake(resizeValue, resizeValue)];
        scaleAnimationDown.toValue  = [NSValue valueWithCGSize:CGSizeMake(1.0f, 1.0f)];

        scaleAnimationDown.completionBlock = ^(POPAnimation *anim, BOOL finished) {
                // Shrink animation done
            if (_attractAttention) {
                [self animatePop];
            }
        };

        [self.layer pop_addAnimation:scaleAnimationDown forKey:@"scaleDownAnimation"];
    };

    [self.layer pop_addAnimation:scaleAnimation forKey:@"scaleUpAnimation"];
}