Search code examples
iosuidynamicanimator

UIDynamicAnimator sequential animation


I'm trying to make buttons that pops out of a menu button (similar to the + button on Path) and using UIDynamicAnimator to animate the buttons snapping in and out. It works and the code is very simple.

    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:sender.superview];

    UIDynamicItemBehavior *resistance = [[UIDynamicItemBehavior alloc] initWithItems:@[_cartBtn, _chatBtn, _eventBtn]];
    resistance.resistance = 5.0f;
    [_animator addBehavior:resistance];

    CGFloat radius = 75;
    CGPoint target = sender.center;
    target.x += radius * cos(GLKMathDegreesToRadians(265));
    target.y += radius * sin(GLKMathDegreesToRadians(265));
    UISnapBehavior *snapBehavior = [[UISnapBehavior alloc] initWithItem:_cartBtn snapToPoint:target];
    [_animator addBehavior:snapBehavior];

    target = sender.center;
    target.x += radius * cos(GLKMathDegreesToRadians(225));
    target.y += radius * sin(GLKMathDegreesToRadians(225));
    UISnapBehavior *snapBehavior2 = [[UISnapBehavior alloc] initWithItem:_chatBtn snapToPoint:target];
    [_animator addBehavior:snapBehavior2];

    target = sender.center;
    target.x += radius * cos(GLKMathDegreesToRadians(185));
    target.y += radius * sin(GLKMathDegreesToRadians(185));
    UISnapBehavior *snapBehavior3 = [[UISnapBehavior alloc] initWithItem:_eventBtn snapToPoint:target];
    [_animator addBehavior:snapBehavior3];

With that, all three buttons pops out together. What I'm asking if it's possible to make the buttons animate out one after another using UIDynamicAnimator, or something else but as simple?


Solution

  • Apparently I just need to add a tiny delay like [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; between addBehavior: