在CAKeyFrameAnimation期间检测碰撞 [英] Detecting collision, during a CAKeyFrameAnimation

查看:75
本文介绍了在CAKeyFrameAnimation期间检测碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在CAKeyFrameAnimation期间沿着路径行进时检测到两个UIImageViews的碰撞?



如果是这样,我已经尝试了多次方法包括检查两个CGRect在动画过程中是否发生碰撞-但找不到在CAKeyFrameAnimation期间执行方法的合适方法,并且无法检测路径和UIImageView的碰撞。

解决方案

您需要从表示层获取属性。它将具有动画过程中存在的信息的最佳近似值。

  view.layer.presentationLayer 

进行访问

有关详细信息,请参阅CALayer / presentationLayer的文档。



当您要检查碰撞时,可以抓住每个对象的presentationLayer,然后访问要测试碰撞的任何属性。检查的确切方法取决于图层的类型,以及是否需要简单的hitTest-ing或深度检查。只有您知道要查找的碰撞时间和碰撞类型。



但是,要在动画制作时访问对象的属性,则需要presentationLayer。 / p>

编辑



您可以随时进行这些检查。您可以在其他操作的上下文中执行此操作,也可以使用NSTimer在一定时间间隔内执行此操作。您甚至可以使用CADisplayLink,同时将您吸引到动画计时器本身中。



如果您使用CADisplayLink,我建议将frameInterval设置为尽可能高的值,并且仍然可以执行此操作

  timer = [CADisplayLink displayLinkWithTarget:self选择器:@selector(checkForCollisions)]; 
//回调适用于每一帧,即每秒60次。
//仅每6帧回调一次(每秒十次)
timer.frameInterval = 6;
[timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

别忘了在完成操作后使计时器失效。


Is it possible to detect the collision of two UIImageViews while one is travelling along a path during a CAKeyFrameAnimation?

If so how is this done, I have tried multiple methods including checking both the CGRects for collision during the animation - but can't find a suitable method for performing a method during a CAKeyFrameAnimation and trying to detect collision of the path and the UIImageView.

解决方案

You need to get the properties from the presentation layer. It will have the best approximation of information that exists during animation. Access it by

view.layer.presentationLayer

Look at the documentation for CALayer/presentationLayer for more details.

When you want to check for collisions, you would grab the presentationLayer of each object, then access whatever properties you want to test for collision. The exact way to check would depend on which type of layer, and whether you wanted simple hitTest-ing or depth checking. Only you know when and what type of collisions you want to look for.

However, to access the properties of an object while it is animating, you need the presentationLayer.

EDIT

You can make these check whenever you want. You can do it in the context of another action, or with an NSTimer to do it at some interval. You can even use CADisplayLink, which while hook you into the animation timer itself.

If you use CADisplayLink, I suggest setting frameInterval at the highest value possible, and still do what you want, so as to not impact performance.

    timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(checkForCollisions)];
    // Callback is for every frame, which is 60 times per second.
    // Only callback every 6 frames (which is ten times per second)
    timer.frameInterval = 6;
    [timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

Don't forget to invalidate the timer when you are done.

这篇关于在CAKeyFrameAnimation期间检测碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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