在碰撞检测中调用CGRectIntersectsRect的问题

时间:2011-04-13 12:03:08

标签: iphone objective-c

当两个imageView相交时我想要碰撞检测。以下是我用于碰撞检测的代码。

- (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的框架。当我们在flakeview图像上拖动此图像时,方法

if(CGRectIntersectsRect(flakeView.frame,dragger.frame))

没有被调用。 任何人都可以帮助这方面..

提前致谢。

1 个答案:

答案 0 :(得分:0)