jow*_*wie 6 core-animation objective-c ipad caanimation ios
我有两个旋转动画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"];
}
Run Code Online (Sandbox Code Playgroud)
由于CFTimeInterval被定义为double,我尝试将动画组的持续时间设置为DBL_MAX,但这会阻止动画组运行.但是,如果我将它设置为一个大数字,如10000,它运行正常.我可以在a的持续时间内使用的最大数字是多少CAAnimationGroup,以确保它尽可能接近无穷大?
更新:似乎如果我输入一个非常大的值,DBL_MAX / 4.0然后它冻结一秒钟,然后开始动画.如果我输入值,DBL_MAX / 20.0则开头的冻结要小得多.似乎在持续时间内具有如此大的值会导致它冻结.除了在持续时间内使用非常大的值之外,还有更好的方法吗?
我现在面临着完全相同的问题...我希望有人证明我错了,但我认为处理这种情况的唯一合理方法是将第一个动画移动到 a CATransaction,并使用以下方法将其与自动反转动画链接起来:
[CATransaction setCompletionBlock:block];
Run Code Online (Sandbox Code Playgroud)
这并不理想,但完成了工作。
关于您关于从后台返回时动画暂停的问题,这是 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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1418 次 |
| 最近记录: |