【问题标题】:How to use CABasicAnimation to move from point to another point?如何使用 CABasicAnimation 从一个点移动到另一个点?
【发布时间】:2012-02-10 07:06:31
【问题描述】:

我正在使用CABasicAnimation 在我的应用程序中从一个点到另一个点执行动画移动对象,但是在动画完成移动到目标点后,它始终显示在起点。如何防止动画结束后在起点不显示?

提前致谢,

【问题讨论】:

    标签: iphone objective-c ios caanimation


    【解决方案1】:

    您似乎忘记为您的对象设置实际的终点值。使用CABasicAnimation,您可以添加动画相应地设置属性。

    【讨论】:

    • 这是我的代码:CABasicAnimation *translateAnim = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"]; translateAnim.duration = 1.0; translateAnim.fromValue = [NSNumber numberWithFloat:0.0f]; translateAnim.toValue = [NSNumber numberWithFloat:-100.f]; [layer addAnimation:translateAnim forKey:@"move"];
    【解决方案2】:

    试试这个

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    CGRect frame=objectName.frame;
    frame.origin.x=frame.origin.x-20;//change the point according to you
    frame.origin.y=frame.origin.y-20;//change the point according to you
    objectName.frame=frame;
    [UIView commitAnimations];
    

    【讨论】:

    • 这段代码运行良好,但我实际上想使用 Core Animation 来执行复杂的动画。例如,在用户定义的路径上移动对象。
    • 这样你就必须在动画结束后改变对象的位置,这样每当你的动画完成时,你的对象就会保持在目标点
    猜你喜欢
    • 1970-01-01
    • 2017-11-25
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2018-05-19
    • 1970-01-01
    相关资源
    最近更新 更多