我有两个对象:一个是通过动画移动,另一个是用我的手指拖动它时移动的对象。我希望能够使用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,以便检测两个对象何时相交?
答案 0 :(得分:2)
您只需要保留对两个视图的引用。然后你所要做的就是:
if(CGRectIntersectsRect(((CALayer*)basketView.layer.presentationLayer).frame,
((CALayer*)flakeView.layer.presentationLayer).frame)) {
//handle the collision
}