我有两个对象:一个通过动画移动,另一个在我用手指拖动时移动。我希望能够使用 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,以便可以检测两个对象何时相交?
您只需要保留对您的两种观点的引用即可。那么你所要做的就是:
if(CGRectIntersectsRect(((CALayer*)basketView.layer.presentationLayer).frame,
((CALayer*)flakeView.layer.presentationLayer).frame)) {
//handle the collision
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)