3

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

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); ]

这是我用手指移动的对象的代码:

-(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);


}

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

4

1 回答 1

2

您只需要保留对您的两个视图的引用。那么你所要做的就是:

if(CGRectIntersectsRect(((CALayer*)basketView.layer.presentationLayer).frame, 
    ((CALayer*)flakeView.layer.presentationLayer).frame)) {
    //handle the collision
}
于 2012-12-01T00:54:30.953 回答