【问题标题】:Make CABasicAnimation for iOS Running Multiple Times为 iOS 制作多次运行的 CABasicAnimation
【发布时间】:2017-09-06 03:28:37
【问题描述】:

我正在构建一个应用程序,该应用程序将使用简单的 CABasicAnimation 生成一个图像在屏幕上“行走”的动画。我将它设置为在一段时间内步行一定距离,然后停止,直到用户给它更多命令,它会再次继续步行相同的距离和持续时间。我遇到的问题是,第一次之后,图像会停在它应该停在的地方,但它不会继续行走,它会跳回到原来的位置并重新开始。我以为我在原点上正确设置了这个设置,但我想不是。

CABasicAnimation *hover = [CABasicAnimation animationWithKeyPath:@"position"];
    hover.fillMode = kCAFillModeForwards;
    hover.removedOnCompletion = NO;
    hover.additive = YES; // fromValue and toValue will be relative instead of absolute values
    hover.fromValue = [NSValue valueWithCGPoint:CGPointZero];
    hover.toValue = [NSValue valueWithCGPoint:CGPointMake(110.0, -50.0)]; // y increases downwards on iOS
    hover.autoreverses = FALSE; // Animate back to normal afterwards
    hover.duration = 10.0; // The duration for one part of the animation (0.2 up and 0.2 down)
    hover.repeatCount = 0; // The number of times the animation should repeat
    [theDude.layer addAnimation:hover forKey:@"myHoverAnimation"];

【问题讨论】:

    标签: ios xcode uiimageview cabasicanimation


    【解决方案1】:

    您的 from 值设置为零且未更新。

    hover.fromValue = [NSValue valueWithCGPoint:CGPointZero];
    

    你必须每次都用你的 to 值更新这个值。

    在这里,我将您的代码放在一个函数中,您可以在其中更新起点和终点。

    - (void)moveFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {
        CABasicAnimation *hover = [CABasicAnimation animationWithKeyPath:@"position"];
        hover.fillMode = kCAFillModeForwards;
        hover.removedOnCompletion = NO;
        hover.additive = YES; // fromValue and toValue will be relative instead of absolute values
        hover.fromValue = [NSValue valueWithCGPoint:fromPoint];
        hover.toValue = [NSValue valueWithCGPoint:toPoint]; // y increases downwards on iOS
        hover.autoreverses = FALSE; // Animate back to normal afterwards
        hover.duration = 10.0; // The duration for one part of the animation (0.2 up and 0.2 down)
        hover.repeatCount = 0; // The number of times the animation should repeat
        [theDude.layer addAnimation:hover forKey:@"myHoverAnimation"];
    }
    

    您可以通过每次使用新点调用此函数来进一步移动该人。

    [self moveFromPoint:CGPointZero toPoint:CGPointMake(110.0, -50.0)]
    
    [self moveFromPoint:CGPointMake(110.0, -50.0) toPoint:CGPointMake(160.0, -50.0)]
    

    编辑:

    我看到你想以相同的比例移动这个家伙,但每次移动的长度不同。

    在@interface之后添加这个变量:

    @property (nonatomic) CGPoint oldPointOfTheGuy;
    

    并在前一个函数之后添加这个新函数:

    - (void)moveByDistance:(CGFloat)distance {
        CGPoint newPointOfTheGuy = CGPointMake(self.oldPointOfTheGuy.x + 2.2*distance, self.oldPointOfTheGuy.y + distance);
        [self moveFromPoint:self.oldPointOfTheGuy toPoint:newPointOfTheGuy];
        self.oldPointOfTheGuy = newPointOfTheGuy;
    }
    

    并为你 viewDidLoad 中的人设置一个起点:

    self.oldPointOfTheGuy = CGPointMake(110.0, -50)
    

    现在我们将这个家伙的旧位置设置为我们知道他第一次生成的位置。

    从现在开始,每次我们要移动他,我们都会这样称呼:

    [self moveByDistance:20];
    

    这个函数的作用是,因为它已经知道你的 x / y 比率为 2.2,它只是将 20 加到你的旧 y 位置,并将 2.2 * 20 加到你的旧 x 位置。并且每次设置新位置时,都会更新旧位置。

    希望这会有所帮助。

    【讨论】:

    • 我喜欢这里的发展方向,但由于有一些用户交互,他可能并不总是每次移动相同的距离,我可能在我的 OP 中输入了错误的部分。比率/斜率将保持不变,而不是整个距离。有什么建议去哪里吗?
    • 效果很好。我对此有最后一个问题。有没有办法让动画始终正常运行,但是如果他到达屏幕上的某个点,会发生不同的动画吗?它可能永远也达不到这一点,但如果他做到了,那么做吗?
    • iOS 中的一些函数有它们的回调。就像当他们完成时,他们告诉你他们已经完成了。如果有它,您可以知道动画在某个时间点完成,并在该确切时间开始新的动画。然而,这个没有这样的回调。它所拥有的是持续时间。例如,您知道您的动画将在 10 毫秒内结束。所以你可以在 10 毫秒后呼叫另一个。然而,所有这些都是错误的路径。这就是为什么游戏引擎被制作成 iOS 的 Unity 或 SpriteKit 的原因。如果您想制作游戏,这些将是您的最佳选择。
    • 否则你将不得不花费太多时间来控制动画。您必须设置一个框架机制,在其中您每毫秒调用某些函数,您可以在其中每毫秒检查您的家伙的当前位置。这样你就可以知道他在给定时间的确切位置,并在必要时调用另一个动画。然而,这些都是远射。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多