更改正在运行的动画的持续时间(速度) [英] Change duration (speed) on a running animation

查看:176
本文介绍了更改正在运行的动画的持续时间(速度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个无限制的旋转动画,当我第一次启动它时效果非常好。我想要实现的是能够在运行时更改旋转速度。我在animationView中有这个函数:

I am doing an indefinite rotation animation, which works perfectly well when I first start it. What I wanted to achieve was to be able to change the rate of rotation at the runtime. I have this function in animationView:

-(void)startBlobAnimation:(float)deltaT
{
    [UIView beginAnimations:@"Spinning" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:deltaT];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationRepeatCount:FLT_MAX];

    CGAffineTransform rotation = CGAffineTransformMakeRotation(-symmetryAngle);
    blobView.transform = rotation;

    // Commit the changes and perform the animation.
    [UIView commitAnimations];
}

首次启动动画后,使用不同的deltaT值调用它不会有任何影响。如果我在函数的开头添加 [wheelView.layer removeAllAnimations]; ,那么它会成功停止动画,但不会重新启动它。我也尝试使用block命令来启动动画,结果相同。在这一点上我完全感到困惑。有人可以解释一下问题是什么吗?谢谢!

Calling it with different values of deltaT after the animation was first started doesn't have any effect. If I add [wheelView.layer removeAllAnimations]; at the start of the function then it successfully stops the animation but doesn't restart it. I also tried using the block command to start animation, with the same result. I am totally baffled at this point. Could somebody explain what the problem is? Thanks!

推荐答案

经过长时间的努力,我想出了一个似乎完美无缺的解决方案,并在动画之间实现平滑过渡。基本上我只是弄清楚当前的旋转角度并使用它以不同的速率重新启动动画。这里的一个关键点是在最后一行:你必须有 anim.keyPath 那里 - 它不能是Nil(从经验中学习)。这就是新的动画取代旧动画,我想。哦,为了使它更清晰:symmetryAngle是一个旋转,使对象看起来相同,如72度,5倍对称。

After a long struggle I came up with a solution that seems to work perfectly and give a smooth transition between animations. Basically I just figure out the current angle of rotation and use it to restart the animation at a different rate. One critical point here is in the last line: you MUST have that anim.keyPath there - it can't be Nil (as learned from experience). That way the new animation replaces the old animation, I guess. Oh, and to make it more clear: symmetryAngle is a rotation that makes the object look the same, like 72 degrees for 5-fold symmetry.

-(void)startWheelsAnimation:(float)deltaT
{
    float startingAngle = 0.0;

    if(isAnimating) {
        // If animation is in progress then calculate startingAngle to
        // reflect the current angle of rotation
        CALayer *presLayer = (CALayer*)[blobView.layer presentationLayer];
        CATransform3D transform = [presLayer transform];
        startingAngle = atan2(transform.m12, transform.m11);
    }

    isAnimating = YES;

    // Restart the animation with different duration, and so that it starts
    // from the current angle of rotation
    CABasicAnimation * anim = [ CABasicAnimation animationWithKeyPath:@"transform.rotation.z" ] ;
    anim.duration = deltaT;
    anim.repeatCount = CGFLOAT_MAX;
    anim.fromValue = @(startingAngle);
    anim.toValue = @(startingAngle - symmetryAngle) ;
    [blobView.layer addAnimation:anim forKey:anim.keyPath];
}

这篇关于更改正在运行的动画的持续时间(速度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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