Ios CGRectContainsPoint不';t返回YES,但在模拟器上,该点位于图像中
在viewDidLoad中,我正在制作一个名为DeepFryingBasket的imageView。你可以用手指左右移动篮子,但不能上下移动。我看到的第二张图片是一个从屏幕上掉下来的炸薯条。如果薯条和篮子相撞,薯条应该消失,看起来像掉进篮子里一样。这就是为什么我有一个NSTimer,每0.01秒触发一次碰撞检测方法 问题是,当我用篮子吃薯条时,薯条并没有消失。 我确信已调用collisionDetection方法,但编译器从未进入if语句,我不知道为什么。任何帮助都将不胜感激Ios CGRectContainsPoint不';t返回YES,但在模拟器上,该点位于图像中,ios,objective-c,cgrect,Ios,Objective C,Cgrect,在viewDidLoad中,我正在制作一个名为DeepFryingBasket的imageView。你可以用手指左右移动篮子,但不能上下移动。我看到的第二张图片是一个从屏幕上掉下来的炸薯条。如果薯条和篮子相撞,薯条应该消失,看起来像掉进篮子里一样。这就是为什么我有一个NSTimer,每0.01秒触发一次碰撞检测方法 问题是,当我用篮子吃薯条时,薯条并没有消失。 我确信已调用collisionDetection方法,但编译器从未进入if语句,我不知道为什么。任何帮助都将不胜感激 - (void)v
- (void)viewDidLoad
{
[super viewDidLoad];
DeepFryingBasket =[[UIImageView alloc] initWithFrame:CGRectMake(110,468,100,80)];
DeepFryingBasket.image = [UIImage imageNamed:@"DeepFryingBasket"];
[self.view addSubview:DeepFryingBasket];
CollisionDetection = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collisionDetection) userInfo:nil repeats:YES];
[CollisionDetection fire];
}
-(void)collisionDetection{
if (CGRectContainsPoint (FrenchFrie.frame, DeepFryingBasket.center)){
[FrenchFrie removeFromSuperview];
points = points + 1;
dropped = dropped +1;
}
}
我使用以下代码移动篮子:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
touchLocation = [touch locationInView:self.view];
frame = DeepFryingBasket.frame;
if (CGRectContainsPoint(frame, touchLocation)){
DeepFryingBasket.center = CGPointMake(touchLocation.x, DeepFryingBasket.center.y);
}
}
编辑:我还尝试添加
DeepFryingBasket.userInteractionEnabled = YES;
但这并没有多大改变。
我还发现,虽然薯条与动画一起掉落,但图像的来源没有改变。为什么这种情况没有改变?我注意到当我移动篮筐时,它的中心是x,x随着我的手指移动。
我用以下代码移动炸薯条:
-(void)letFrenchFrieFall {
[UIView animateWithDuration:10.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
FrenchFrie.frame = CGRectMake(randomX, (int)[[UIScreen mainScreen] bounds].size.height + 40, FrenchFrie.frame.size.width, FrenchFrie.frame.size.height);
} completion:^(BOOL finished){
[FrenchFrie removeFromSuperview];
}];
}
好的,我找到了我自己问题的答案: 当imageView由于动画而移动时,x坐标和Y坐标设置为动画结束的位置;由于这个原因,不可能用计算机获取它的位置
FrenchFrie.frame
你可以用
FrenchFrieFrame = [[FrenchFrie.layer presentationLayer] frame];
获取当前显示的帧。
不要忘记包括Quartzcore框架并导入它
#import <QuartzCore/QuartzCore.h>
#导入