gpt4 book ai didi

ios - 用流行创造一个无尽的动画?

转载 作者:行者123 更新时间:2023-11-28 18:32:23 24 4
gpt4 key购买 nike

当使用 Coreanimation 框架时,我可以将动画设置为重复。我想将一个按钮设置为“吸引注意力”模式,这应该让他稍微长大和缩小以引起用户的注意。

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

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

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

编辑:

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

__weak typeof(scaleAnimation) weakAnimation = scaleAnimation;

最佳答案

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

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;

关于ios - 用流行创造一个无尽的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25954443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com