gpt4 book ai didi

objective-c - UIImageView 和 CGRectIntersectRect (Xcode)

转载 作者:行者123 更新时间:2023-11-28 23:09:54 26 4
gpt4 key购买 nike

我有 2 个 UIImageViews,其大小为 50x50 和 60x60 像素。

我使用 CGRectIntersectRect 方法来确定一幅图像何时接触另一幅图像。但是这 2 个图像是 2 个球,有时代码确定这些图像在“用你的眼睛”看到它们没有接触时正在接触它们自己。发生这种情况是因为球不会“覆盖”图像的所有空间。所以图像的角落是清晰的,但是代码当然不关心这个。

问题是:我该如何解决?如何确定完全符合球周长的图像帧?

最佳答案

如果您确定 UIImageView 中的图像将始终为圆形(或者 UIImageView 将始终为方形),那么您可以执行以下操作

CGRect frame1 = image1.frame;
CGRect frame2 = image2.frame;
CGPoint center1 = CGPointMake(CGRectGetMidX(frame1), CGRectGetMidY(frame1));
CGPoint center2 = CGPointMake(CGRectGetMidX(frame2), CGRectGetMidY(frame2));

CGFloat dx = center1.x - center2.x;
CGFloat dy = center2.x - center2.y;
float squaredDistance = dx * dx + dy * dy;

CGFloat radius1 = frame1.size.width/2;
CGFloat radius2 = frame2.size.width/2;
CGFloat minDistance = radius1 + radius2;
if (squaredDistance <= minDistance * minDistance) {
//Intersect
} else {
//Do not intersect
}

我试图让所有东西都参数化,但是,当然,如果 UIImageView 的框架是固定的,那么您可以使用例如 #define 指令编写更简单的代码.

例如,如果你知道第一张图片是 50x50,第二张是 60x60,你可以这样写

#define RADIUS1 25
#define RADIUS2 30

并简化您的代码。

我假设每个圆圈都刻在 UIImageView 的框架中。如果不是,那么您应该知道正确测量半径的确切比率。 (到目前为止,在框架中刻上圆圈更容易。)

关于objective-c - UIImageView 和 CGRectIntersectRect (Xcode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8569098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com