如何在iPhone中使用Cabasicanization沿x方向移动视图或标签

如何在iPhone中使用Cabasicanization沿x方向移动视图或标签,iphone,Iphone,我使用下面的代码在x方向将标签从一个位置移动到另一个位置 CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; theAnimation.duration=1; theAnimation.repeatCount=1; theAnimation.autoreverses=NO; theA

我使用下面的代码在x方向将标签从一个位置移动到另一个位置

CABasicAnimation *theAnimation; 
    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    theAnimation.duration=1;
    theAnimation.repeatCount=1;
    theAnimation.autoreverses=NO;
    theAnimation.fromValue=[NSNumber numberWithFloat:0];
    theAnimation.toValue=[NSNumber numberWithFloat:80];


    [lbl.layer addAnimation:theAnimation forKey:@"animateLayer"];
但在本例中,在动画结束时,标签将移回其原始位置。如何确保它保持在移动的位置


有没有什么好方法可以不用定时器,不用我们自己改变坐标。

动画完成后,它会被删除。这就是为什么它会反弹。将以下内容添加到动画中:

theAnimation.removedOnCompletion = NO;
theAnimation.fillMode = kCAFillModeForwards;

这将防止动画被删除,并告知动画在完成后保持其最终状态。

此处有2项需要更新。表示层和模型。CABasicAnimation仅更改表示层,从不更新模型。因此,当动画中的表示层完成时,它将消失,您将看到带有模型值的视图。动画完成后,只需使用新值更新模型

[图层设置值:toValue forKeyPath:keyPath];
看一看我写的一个实用程序,它可以帮助我们实现这个,嗯,基本化

用法:

[HMBasicAnimation doAnimation:myView.layer // layer youre updating
                      toValue:myView.frame.size.width/2 // your value
                     duration:1.5 // duration
                 delaySeconds:1 // animation delay (good for chaining animations
                      keyPath:HMBasicAnimation_TRANSLATION_X]; // what you're changing, several available