xcode 两个图像在实际接触之前相交

发布于 2024-11-05 22:34:12 字数 1273 浏览 1 评论 0原文

可能的重复:
两个图像碰撞的问题

大家好,我是法国人,请原谅我我的英语:所以我有两个图像(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皓月长歌 2024-11-12 22:34:12

您是说 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文