0
我想在兩個imageView交叉時碰撞檢測。以下是我用於碰撞檢測的代碼。在碰撞檢測中調用CGRectIntersectsRect的問題
- (void)onTimer
{
// build a view from our flake image
flakeView = [[UIImageView alloc] initWithImage:flakeImage];
flakeView.backgroundColor=[UIColor blueColor];
// use the random() function to randomize up our flake attributes
int startX =round(random() % 460);
printf("\n ===== startX :%d",startX);
int endX = round(random() % 460);
printf("\n ===== endX :%d",endX);
double scale = 1/round(random() % 100) - 1.0;
double speed = 1/round(random() %100) + 1.0;
// set the flake start position
flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
flakeView.alpha = 1.0;
// put the flake in our main view
[mView addSubview:flakeView];
[UIView beginAnimations:nil context:flakeView];
[UIView setAnimationDuration:20 * speed];
if(dragger.setFrame==YES)
{
printf("\n =====dragger.frame.origin.x:%f",dragger.frame.origin.x);
printf("\n =====dragger.frame.origin.y:%f",dragger.frame.origin.y);
flakeView.frame = CGRectMake(dragger.frame.origin.x, dragger.frame.origin.y, 25.0 * scale, 25.0 * scale);
printf("\n =====flakeView.frame.origin.x:%f",flakeView.frame.origin.x);
printf("\n =====flakeView.frame.origin.y:%f",flakeView.frame.origin.y);
if(CGRectIntersectsRect(flakeView.frame,dragger.frame))
{
NSLog(@"\nRect is Intersecting");
}
dragger.setFrame=NO;
}
else
{
flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale);
}
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
在這個drager框架是一個inageView框架。當我們將此圖片拖到片狀圖像上時,方法
if(CGRectIntersectsRect(flakeView.frame,dragger.frame))
沒有被調用。 任何人都可以在這方面幫助..
在此先感謝。
你能證明你的程序的輸出?有很多打印出來的調試信息,這會有很大的幫助。 – jv42 2011-04-13 12:16:43