CABasicAnimation innerhalb einer "Duration" stoppen und wieder von vorne starten

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • CABasicAnimation innerhalb einer "Duration" stoppen und wieder von vorne starten

    Hallo,

    in meiner Methode habe ich folgenden Code. Dieser soll erstens mal einen den Ball rollen lassen. Dies klappt auch beim ersten klick.
    Wenn man nun die Methode zum zweiten mal innerhalb der Duration (bzw. Repeatings) aufruft soll die Animation gestoppt werden und wieder von vorne beginnen.
    Das klappt aber nicht. Sie wird nur gestoppt und es passiert 10 Sekunden(5x2) nichts. Wenn ich nach dieser Zeit nochmal den Button klicke dreht sich der Ball normal.

    Danke schonmal für eure Hilfe :)

    Quellcode

    1. - (void) setAnimation {
    2. //Stoppt die Animation
    3. [buttonLayer removeAllAnimations]

    Quellcode

    1. CABasicAnimation *theAnimation;
    2. theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    3. theAnimation.duration=2;
    4. theAnimation.autoreverses=NO;
    5. theAnimation.repeatCount = 5;
    6. theAnimation.fromValue = [NSNumber numberWithFloat:(0 * M_PI / 180)];
    7. theAnimation.toValue = [NSNumber numberWithFloat:(360 * M_PI / 180)];
    8. [buttonLayer addAnimation:theAnimation forKey:@"ballRotation"];
    9. }
    in Bearbeitung
  • Sollte eigentlich funktionieren.

    Evtl. darf man die vorhandenen Animation und die neuen Animationen nicht in einem Event-Loop verändern, daher versuche es mal so:

    Quellcode

    1. - (void)addAnimation
    2. {
    3. CABasicAnimation *theAnimation;
    4. theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    5. theAnimation.duration=2;
    6. theAnimation.autoreverses=NO;
    7. theAnimation.repeatCount = 5;
    8. theAnimation.fromValue = [NSNumber numberWithFloat:(0 * M_PI / 180)];
    9. theAnimation.toValue = [NSNumber numberWithFloat:(360 * M_PI / 180)];
    10. [buttonLayer addAnimation:theAnimation forKey:@"ballRotation"];
    11. }
    12. - (void) setAnimation
    13. {
    14. //Stoppt die Animation
    15. [buttonLayer removeAllAnimations];
    16. // Setzt die neue Animation im nächsten Event-Loop
    17. [self performSelectorOnMainThread:@selector(addAnimation) withObject:nil waitUntilDone:NO];
    18. }
    Alles anzeigen