gpt4 book ai didi

另一个 View 范围内的 iOS View

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

我正在尝试检查一个 View 是否在另一个 View 的范围内以执行某些操作。这是我的代码:

CGRect movingView = [self.viewController.view convertRect:recognizer.view.frame toView:self.viewController.view];

for (NSUInteger i = 0; i < arrayOfViewsToCheck.count; i++)
{
UIView *view = resultArray [0];

CGRect boxframe = [self.viewController.view convertRect:view.frame toView:self.viewController.view];

if (CGRectIntersectsRect(movingView.frame, view.frame) )
{
isInBounds = YES;
}
}

但是当我执行我的代码时,例如我有这两个 View :

(CGRect) $6 = origin=(x=569, y=513) size=(width=76, height=100)

(CGRect) $7 = origin=(x=358, y=520) size=(width=116, height=100)

在彼此的范围内,但我不知道我做错了什么,因为它们似乎不在我的代码中。

如果可以让我知道我在尝试检测 View 是否在彼此的边界内做错了什么,我将不胜感激

最佳答案

您对 CGRectIntersectsRect 的使用是正确的,但示例中的矩形实际上并不相交。它们的外观如下:

- (void)viewDidLoad
{
[super viewDidLoad];

CGRect rect1 = CGRectMake(569,513,76,100);
CGRect rect2 = CGRectMake(368,520,116,100);

UIView *view1 = [[UIView alloc] initWithFrame:rect1];
UIView *view2 = [[UIView alloc] initWithFrame:rect2];

view1.backgroundColor = [UIColor redColor];
view2.backgroundColor = [UIColor blueColor];

[self.view addSubview:view1];
[self.view addSubview:view2];
}

non-intersecting rectangles

稍微修改后的版本:

- (void)viewDidLoad
{
[super viewDidLoad];

CGRect rect1 = CGRectMake(469,513,76,100); /* Note 469 instead of 569 */
CGRect rect2 = CGRectMake(368,520,116,100);

UIView *view1 = [[UIView alloc] initWithFrame:rect1];
UIView *view2 = [[UIView alloc] initWithFrame:rect2];

view1.backgroundColor = [UIColor redColor];
view2.backgroundColor = [UIColor blueColor];

[self.view addSubview:view1];
[self.view addSubview:view2];

if (CGRectIntersectsRect(rect1, rect2) ) {
NSLog(@"Yay we intersect!");
}
}

enter image description here

关于另一个 View 范围内的 iOS View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299573/

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