我有两个对象:一个是通过动画移动的,另一个是当我用手指拖动它时移动的。我想用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动画的表示层,以便检测两个对象何时相交?
最佳答案
你只需要保留对两个视图的引用。那么你要做的就是:
if(CGRectIntersectsRect(((CALayer*)basketView.layer.presentationLayer).frame,
((CALayer*)flakeView.layer.presentationLayer).frame)) {
//handle the collision
}