Robert Gummesson
Robert Gummesson

Reputation: 5666

iOS: Draw line between two animating views

I have two UIViews with two different animations using UIView.animateWithDuration. The first animation starts right away, the second starts after a 0.5s delay.

How do I draw and animate a line between them like the below example:

animation

My first attempt was to draw the line as a CGPath and then animate it using CABasicAnimation. This works if the two views (or shapes in that test) animates at the same time, not when the second animation has a delayed start.

I've then been looking into grabbing the values of the UIView frame positions on a continuous basis. That would enable me to redraw my line on each animation frame but I couldn't find any way of doing that either.

So... How do I achieve this?

Upvotes: 1

Views: 795

Answers (1)

pNre
pNre

Reputation: 5376

CADisplayLink is probably what you are looking for.

Add an update method to your class and perform the animations there:

- (void)update {
    // animate view 1
    CGRect frame = view1.frame;
    frame.origin.y += 1;
    view1.frame = frame;
    // animate view 2
    // draw the line/animate another view
}

When you want to start the animation, do:

displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

Once the animation is complete remove the displayLink from the run loop.

Upvotes: 3

Related Questions