跟踪核心动画动画 [英] Tracking a Core Animation animation

查看:90
本文介绍了跟踪核心动画动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个在屏幕上移动的圆圈。这些圆圈都是包含其他UIView的UIView。每个圆圈之外的区域是透明的。

I have two circles which move around the screen. The circles are both UIViews which contain other UIViews. The area outside each circle is transparent.

我写了一个函数创建一个CGPath,它连接两个圆形的四边形。我在跨越整个屏幕的透明CALayer中填充此路径。因为这个图层在两个圆形UIViews的后面,所以它们似乎是连接它们的。

I have written a function to create a CGPath which connects the two circles with a quadrilateral shape. I fill this path in a transparent CALayer which spans the entire screen. Since the layer is behind the two circular UIViews, it appears to connect them.

最后,两个UIViews使用Core Animation动画。此动画期间,两个圈子的位置和大小都会改变。

Finally, the two UIViews are animated using Core Animation. The position and size of both circles change during this animation.

到目前为止,我唯一有效的方法是使用NSTimer定期中断动画,然后根据圆的演示层的位置重新计算和绘制光束。

So far the only method that I have had any success with is to interrupt the animation at regular intervals using an NSTimer, then recompute and draw the beam based on the location of the circle's presentationLayer. However, the quadrilateral lags behind the circles when the animation speeds up.

有没有更好的方法来使用Core Animation完成这项工作?或者我应该避免Core Animation,并使用NSTimer实现我自己的动画。

Is there a better way to accomplish this using Core Animation? Or should I avoid Core Animation and implement my own animation using an NSTimer?

推荐答案

我用动画的层而不是视图。

I faced a similar problem. I used layers instead of views for the animation. You could try something like this.


  1. 将每个元素绘制为CALayer,并将它们作为子图层包含在UIVIew的图层中。 UIViews更容易动画,但你将有更少的控制。注意,对于任何视图,你可以得到它的图层与[视图层];

  2. 为您的四边形创建自定义子图层。此图层应具有一个属性或若干要为此图层设置动画的属性。让我们调用这个属性customprop。因为它是一个自定义图层,所以你想重绘动画的每一帧。对于要计划动画的属性,您的自定义图层类应该返回YES needsDisplayForKey :.

  3. 将所有动画(包括圆和四边形)放置在所有的框架中,这样就可以在框架中调用 - 同一笔交易;






对于社交圈,您可以使用CALayers并设定内容,如果是图片,标准方式:


For the circles you can probably use CALayers and set the content, if it is an image, the standard way:

layer.contents = [UIImage imageNamed:@"circle_image.png"].CGImage;

现在,对于四层,子类CALayer并实现这种方式:

Now, for the quad layer, subclass CALayer and implement this way:

- (void)drawInContext:(CGContextRef)theContext{
  //Custom draw code here
}   
+ (BOOL)needsDisplayForKey:(NSString *)key{
   if ([key isEqualToString:@"customprop"])
      return YES;
    return [super needsDisplayForKey:key];
}   

交易如下:

[CATransaction begin];
CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"customprop"];

theAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(1000, 1000)];
theAnimation.duration=1.0;
theAnimation.repeatCount=4;
theAnimation.autoreverses=YES;
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
theAnimation.delegate = self;
[lay addAnimation:theAnimation forKey:@"selecting"];

[CATransaction setValue:[NSNumber numberWithFloat:10.0f]
                     forKey:kCATransactionAnimationDuration];
circ1.position=CGPointMake(1000, 1000);
circ2.position=CGPointMake(1000, 1000);
[CATransaction commit];

现在所有的绘制例程都会同时发生。确保你的drawInContext:实现是快速的。否则动画将滞后。

Now all the draw routines will happen at the same time. Make sure your drawInContext: implementation is fast. Otherwise the animation will lag.

将每个子图层添加到UIViews的图层后,rememeber调用[layer setNeedsDisplay]。它不会自动调用。

After adding each sublayer to the UIViews's layer, rememeber to call [layer setNeedsDisplay]. It does not get called automatically.

我知道这有点复杂。然而,所得到的动画比在每次调用时使用NSTimer和重绘更好。

I know this is a bit complicated. However, the resulting animations are better than using a NSTimer and redrawing on each call.

这篇关于跟踪核心动画动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆