xcode 两个图像在实际接触之前相交
可能的重复:
两个图像碰撞的问题
大家好,我是法国人,请原谅我我的英语:所以我有两个图像(flakeView 和 viewToRotate),一个正在移动,另一个静态在屏幕中央。我想要的是:如果 flakeView 和 viewToRotate 发生碰撞:做点什么。但问题是它们在实际接触之前就相交了,我不知道问题出在哪里。 这是代码:
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
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
if (CGRectIntersectsRect(flakeView.frame, viewToRotate.frame) == 1){
//do something
}
}
请问我该如何解决这个问题?
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
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
if (CGRectIntersectsRect(flakeView.frame, viewToRotate.frame) == 1){
//do something
}
}
How can I solve this please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是说 CGRectIntersectsRect 在图像接触之前为 true 吗?
如果 UIImageView 中的图形小于框架,则 UIImageView 实际上会在可见图像接触之前接触甚至相交。我想知道这是否是你的问题。
如果是这样,解决方案就是尽可能多地使用框架。
另一个条件是 UIImageView 内有一个非矩形形状。如果您有圆形图像,则当圆形不接触(但它们的框架接触)时 CGRectIntersectsRect 可以返回 true。
上述问题的解决方案是用代码补充 CGRectIntersectsRect 来检查所描绘图像的边界,或者创建您自己的碰撞检测代码(我的偏好)来处理形状周围的独特情况。
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.