访问动画的presentationLayer以检测碰撞 [英] Accessing presentationLayer of an animation to detect collisions

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

问题描述

我有两个对象:一个是通过动画移动的,另一个是用我的手指拖动它时移动的对象。我希望能够使用CGIntersectsRect检测两者何时碰撞。但是,我听说为了使用动画执行此操作,我需要访问表示层以从中获取值。但是,我不知道该怎么做。这是我的动画代码:

I have two objects: one that is moving by animation, and another that is moving when I drag it around with my finger. I want to be able to detect when the two "collide" using CGIntersectsRect. However, I've heard that in order to do this with animations, I need to access the presentation layer to grab values from there. I have no idea how to go about doing this, however. This is the animation code I have:

UIImage *flakeImage = [UIImage imageNamed:@"apple.png"];
UIImageView *flakeView = [[UIImageView alloc] initWithImage:flakeImage];
flakeView.frame = CGRectMake(200, -25.0, 25.0, 25.0);
[self.view addSubview:flakeView];

[UIView beginAnimations:nil context:(flakeView)];
[UIView setAnimationDuration:2];

flakeView.frame = CGRectMake(200, 800.0, 25.0, 25.0); ]

这是我用手指移动的物体的代码:

and here is the code for the object I move with my finger:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // get current touch location
    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint point = [touch locationInView:self.view];
    // update location of the image
    basketView.center = CGPointMake(point.x, basketView.center.y);


}

如何访问presentationLayer的flakeView动画,以便我可以检测两个对象何时相交?

How can I access the presentationLayer of the flakeView animation so that I can detect when the two objects intersect?

推荐答案

您只需要保留对两个对象的引用观点。然后你所要做的就是:

You simply need to keep around a reference to both of your views. Then all you have to do is:

if(CGRectIntersectsRect(((CALayer*)basketView.layer.presentationLayer).frame, 
    ((CALayer*)flakeView.layer.presentationLayer).frame)) {
    //handle the collision
}

这篇关于访问动画的presentationLayer以检测碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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