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

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

问题描述

我正在做一个无限旋转动画,当我第一次启动它时效果很好.我想要实现的是能够在运行时更改旋转速率.我在 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 是一种使对象看起来相同的旋转,例如 5 重对称的 72 度.

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天全站免登陆