iOS的 - 在一个不确定的动画时间更改 [英] iOS - Changing duration on an indefinite animation

查看:150
本文介绍了iOS的 - 在一个不确定的动画时间更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重载的UIImageView(animationView),其中添加其他的UIImageView作为子视图动画(blobView)。我做一个不确定旋转动画,当我第一次启动它工作得很好。我想实现是能够在运行时改变旋转速率。我在animationView这个功能:

   - (无效)startBlobAnimation:(浮动)的DeltaT
{
    [UIView的beginAnimations:@纺背景:无];
    [UIView的setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView的setAnimationDuration方式:DeltaT];
    [UIView的setAnimationBeginsFromCurrentState:YES];
    [UIView的setAnimationRepeatCount:FLT_MAX];    CGAffineTransform旋转= CGAffineTransformMakeRotation(-symmetryAngle);
    blobView.transform =旋转;    //提交更改并执行动画。
    [UIView的commitAnimations];
}

与DeltaT的值不同调用后动画首次启动没有任何影响。如果我添加 [wheelView.layer removeAllAnimations]; 在函数的开始,然后它成功地停止动画,但不会重新启动它。我使用块命令启动动画,具有相同的结果也试过。我在这一点上完全莫名其妙。可能有人解释是什么问题?谢谢!


<格CLASS =h2_lin>解决方案

一个长期的斗争,我想出了,似乎完美地工作,给动画之间的平滑过渡的解决方案后。基本上,我只是想出旋转当前角度,并使用它以不同的速率来重新启动动画。这里的一个关键点是在最后一行:你必须有一个 anim.keyPath 那里 - 它不能是零(因为从经验教训)。这样,新的动画取代了旧的动画,我猜。哦,使其更清楚:symmetryAngle是旋转,使物体看起来是一样的,像72度的5倍对称性

   - (无效)startWheelsAnimation:(浮动)的DeltaT
{
    浮startingAngle = 0.0;    如果(isAnimating){
        //如果动画正在进行然后计算startingAngle到
        //反映旋转当前角度
        CALayer的* presLayer =(CALayer的*)[blobView.layer presentationLayer]。
        CATransform3D变换= [presLayer变换]
        startingAngle = ATAN2(transform.m12,transform.m11);
    }    isAnimating = YES;    //重新启动与不同持续时间的动画,所以它启动
    从当前的旋转角度//
    CABasicAnimation *动画= [CABasicAnimation animationWithKeyPath:@transform.rotation.z];
    anim.duration = DeltaT的;
    anim.repeatCount = CGFLOAT_MAX;
    anim.fromValue = @(startingAngle);
    anim.toValue = @(startingAngle - symmetryAngle);
    [blobView.layer addAnimation:动画forKey:anim.keyPath];
}

I have an overloaded UIImageView (animationView) where I add another UIImageView as a subview to be animated (blobView). 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];
}

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!

解决方案

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

这篇关于iOS的 - 在一个不确定的动画时间更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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