gpt4 book ai didi

ios - CAAnimationGroup 的最大持续时间值 (CFTimeInterval) 是多少?

转载 作者:可可西里 更新时间:2023-11-01 03:59:28 27 4
gpt4 key购买 nike

我的 CAAnimationGroup 中有两个旋转动画,一个从零开始,另一个从该状态开始重复和自动反转:

- (void)addWobbleAnimationToView:(UIView *)view amount:(float)amount speed:(float)speed
{
NSMutableArray *anims = [NSMutableArray array];

// initial wobble
CABasicAnimation *startWobble = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
startWobble.toValue = [NSNumber numberWithFloat:-amount];
startWobble.duration = speed/2.0;
startWobble.beginTime = 0;
startWobble.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[anims addObject:startWobble];

// rest of wobble
CABasicAnimation *wobbleAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
wobbleAnim.fromValue = [NSNumber numberWithFloat:-amount];
wobbleAnim.toValue = [NSNumber numberWithFloat:amount];
wobbleAnim.duration = speed;
wobbleAnim.beginTime = speed/2.0;
wobbleAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
wobbleAnim.autoreverses = YES;
wobbleAnim.repeatCount = INFINITY;
[anims addObject:wobbleAnim];

CAAnimationGroup *wobbleGroup = [CAAnimationGroup animation];
wobbleGroup.duration = DBL_MAX; // this stops it from working
wobbleGroup.animations = anims;

[view.layer addAnimation:wobbleGroup forKey:@"wobble"];
}

由于 CFTimeInterval 被定义为 double 值,我尝试将动画组的持续时间设置为 DBL_MAX,但这会阻止动画组运行。但是,如果我将它设置为一个很大的数字,例如 10000,它运行良好。我可以在 CAAnimationGroup 的持续时间内使用的最大数字是多少,以确保它尽可能接近无穷大?

更新: 看来,如果我输入一个非常大的值,例如 DBL_MAX/4.0,它会卡住一秒钟,然后开始动画。如果我输入值 DBL_MAX/20.0,那么开始时的卡住会小很多。似乎持续时间有这么大的值导致它卡住。除了在持续时间内使用非常大的值之外,还有更好的方法吗?

最佳答案

我现在面临着完全相同的问题...我希望有人能证明我错了,但我认为处理这种情况的唯一合理方法是将第一个动画移动到 CATransaction ,并使用以下方法将其与自动反转动画链接起来:

[CATransaction setCompletionBlock:block];

这并不理想,但可以完成工作。

关于您关于从后台返回时动画被暂停的问题,这是 CoreAnimation 框架的经典限制,已经提出了许多解决方案。我解决它的方法是通过随机化 timeOffset 属性,简单地在动画的随机点重置动画。用户无法准确判断动画状态应该是什么,因为应用程序处于后台。下面是一些可以提供帮助的代码(查找 //RANDOMIZE!! 注释):

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(startAnimating)
name:UIApplicationWillEnterForegroundNotification
object:[UIApplication sharedApplication]];

...

for (CALayer* layer in _layers)
{
// RANDOMIZE!!!
int index = arc4random()%[levels count];
int level = ...;
CGFloat xOffset = ...;

layer.position = CGPointMake(xOffset, self.bounds.size.height/5.0f + yOffset * level);

CGFloat speed = (1.5f + (arc4random() % 40)/10.f);
CGFloat duration = (int)((self.bounds.size.width - xOffset)/speed);

NSString* keyPath = @"position.x";
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:keyPath];

anim.fromValue = @(xOffset);
anim.toValue = @(self.bounds.size.width);
anim.duration = duration;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
// RANDOMIZE!!
anim.timeOffset = arc4random() % (int) duration;
anim.repeatCount = INFINITY;

[layer removeAnimationForKey:keyPath];
[layer addAnimation:anim forKey:keyPath];
[_animatingLayers addObject:layer];
}

关于ios - CAAnimationGroup 的最大持续时间值 (CFTimeInterval) 是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15664976/

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