xcode two images intersects before they actually touch [duplicate]
Possible Duplicate:
problem with collision of two images
HI everyone I'm french so scuse me for my english: SO I have two images(flakeView and viewToRotate) one that is moving, the other static in the center of the screen.What I want is : if flakeView and viewToRotate collide : do something. But the problem is that they intersects before they actually touch and I don't know where is the problem. here is the code:
UIImageView *flakeView = [[UIImageView alloc] initWithImage:flakeImage];
// use the random() function to randomize up our flake attributes
int startX = round(random() % 480);
// set the flake start position
flakeView.center = CGPointMake(startX, -20.0);
flakeView.alpha = 1;
CGRect frame = CGRectMake(startX, -20.0, 30, 30);
flakeView.frame = frame;
// put the flake in our main view
[self.view addSubview:flakeView];
[UIView beginAnimations:nil context:flakeView];
// set up how fast the flake will fall
[UIView setAnimationDuration:7 ];
// set the postion where flake will move to
flakeView.center = viewToRotate.center;
// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIVie开发者_运维知识库w setAnimationDelegate:self];
[UIView commitAnimations];
if (CGRectIntersectsRect(flakeView.frame, viewToRotate.frame) == 1){
//do something
}
}
How can I solve this please ?
Are you saying that CGRectIntersectsRect is true before the images touch?
If the graphic within the UIImageView is smaller than the frame the UIImageViews will, in fact, touch and even intersect before the visible images touch. I wonder if that's your problem.
If so, the solution would be to use as much of the frame as possible.
The other condition is that you have a non-rectangular shape inside your UIImageView. If you have the image of circle then CGRectIntersectsRect can return true when the circles are not touching (but their frames do).
The solution to the above is to either supplement CGRectIntersectsRect with code to check the bounds of the image being portrayed or to create your own collision detection code (my preference) to deal with the unique cases surrounding your shape.
精彩评论