创建流行无尽的动画? [英] Create an endless animation with pop?

查看:134
本文介绍了创建流行无尽的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Coreanimation框架我可以设置动画重复。我想一个按钮设置为吸引眼球模式,这应该让他有少量以获得用户关注度增长和收缩。

我已经链接的成长,并通过竣工块收缩动画。现在的问题是,如果我怎么能开始从第二个动画的完成块中的第一个动画。

我得到以下警告这有一定道理。什么是一个优雅的解决这个问题呢?我不是创造的东西计时器这样的粉丝。


  

捕获'scaleAnimation强烈该块是可能导致
  一个保留周期


   - (无效)attractAttention:(BOOL)标志{
    _attractAttention =标志;
    浮resizeValue = 1.2F;        //动画成长
    POPSpringAnimation * scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    scaleAnimation.fromValue = [NSValue valueWithCGSize:CGSizeMake(1.0F,1.0F)];
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(resizeValue,resizeValue)];
    scaleAnimation.completionBlock = ^(POPAnimation *阿尼姆,BOOL完){
            //增长做动画
        POPSpringAnimation * scaleAnimationDown = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
        scaleAnimationDown.fromValue = [NSValue valueWithCGSize:CGSizeMake(resizeValue,resizeValue)];
        scaleAnimationDown.toValue = [NSValue valueWithCGSize:CGSizeMake(1.0F,1.0F)];        scaleAnimationDown.completionBlock = ^(POPAnimation *阿尼姆,BOOL完){
                //收缩做过动画
            如果(_attractAttention){
                [self.layer pop_addAnimation:scaleAnimation forKey:@scaleUpAnimation];
            }
        };        [self.layer pop_addAnimation:scaleAnimationDown forKey:@scaleDownAnimation];
    };    [self.layer pop_addAnimation:scaleAnimation forKey:@scaleUpAnimation];
}

编辑:

我也试图创建动画的弱引用。这消除了错误,但是动画不工作了:

  __弱的typeof(scaleAnimation)weakAnimation = scaleAnimation;


解决方案

您也可以使用属性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;
[层pop_addAnimation:scaleAnimation forKey:@规模];

更妙的是,如果你检查这个类<一个href=\"https://github.com/facebook/pop/blob/master/pop/POPAnimation.h\">https://github.com/facebook/pop/blob/master/pop/POPAnimation.h你会看到有一个repeatForever属性,因此你可以用它代替的repeatCount:

  scaleAnimation.repeatForever = YES;

When using the Coreanimation framework I can set an animation to repeat. I want to set a button to an "attract attention" mode which should make him grow and shrink by a small amount to get the users attention.

I already chained the grow and shrink animations via completion blocks. The question is if and how I can start the first animation from the second animation's completion block.

I do get the following warning which does make sense. What is an elegant solution to this problem? I'm not a fan of creating timers for stuff like this.

Capturing 'scaleAnimation' strongly in this block is likely to lead to a retain cycle

- (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"];
}

Edit:

I also tried to create a weak reference of the animation. This removes the error, but the animations do not work anymore:

__weak typeof(scaleAnimation) weakAnimation = scaleAnimation;

解决方案

You can also use the properties autoreveres and repeatCount to achieve the same result without use blocks. It reduces the complexity:

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"];

Even better, if you check this class https://github.com/facebook/pop/blob/master/pop/POPAnimation.h you will see there is a repeatForever property, so you could replace repeatCount by it:

scaleAnimation.repeatForever=YES;

这篇关于创建流行无尽的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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