开发者

problem in calling CGRectIntersectsRect in collison detection

I want collision detection when two imageViews intersect. Following is the code that I used for collision detection.

- (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开发者_如何学Go =====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];

 }

In this drager frame is frame for one inageView . When we drag this image over flakeview image the method

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

is not getting called. Can anyone help in this regard..

Thanks in advance.


You can refer here....

sample code for collision detection in iPhone sdk

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜