Detecting collision, during a CAKeyFrameAnimation

折月煮酒 提交于 2019-12-06 09:37:31

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!