简体   繁体   English

iOS:在两个动画视图之间绘制线条

[英]iOS: Draw line between two animating views

I have two UIViews with two different animations using UIView.animateWithDuration. 我有两个使用UIView.animateWithDuration的UIViews和两个不同的动画。 The first animation starts right away, the second starts after a 0.5s delay. 第一个动画立即开始,第二个动画在延迟0.5秒后开始。

How do I draw and animate a line between them like the below example: 我如何在它们之间绘制和绘制一条线,如下例所示:

动画

My first attempt was to draw the line as a CGPath and then animate it using CABasicAnimation. 我的第一次尝试是将该线绘制为CGPath,然后使用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. 我一直在研究不断抓住UIView框架位置的值。 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? 那么......我怎么做到这一点?

CADisplayLink is probably what you are looking for. CADisplayLink可能就是你想要的。

Add an update method to your class and perform the animations there: 向您的班级添加update方法并在那里执行动画:

- (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. 动画完成后,从运行循环中删除displayLink

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM