gpt4 book ai didi

ios - 如何使用 CGPoint 获取 UIScrollView 的 subview

转载 作者:行者123 更新时间:2023-11-28 21:56:41 24 4
gpt4 key购买 nike

我有一个 UIScrollView,其中排列着许多大小相等的矩形 subview 。然后我需要能够将 CGPoint 传递给那个 UIScrollView 并且我希望它给我包含 CGPoint 的矩形 subview 。这基本上是 hitTest:event,除了 hitTest:event: 一旦 CGPoint 超出UIScrollView 限制并且不查看其实际内容。

大家都在做什么?如何在 UIScrollView content View 上进行“ HitTest ”?

下面是一些代码来说明这个问题:

NSArray *rectangles = [self getBeautifulRectangles];

CGFloat rectangleLength;
rectangleLength = 100;

// add some rectangle subviews
for (int i = 0; i < rectangles.count; i++) {
UIView *rectangle = [rectangles objectAtIndex:i];
[rectangle setFrame:CGRectMake(i * rectangleLength, 0, rectangleLength, rectangleLength)];
[_scrollView addSubview:rectangle];
}
[_scrollView setContentSize:CGSizeMake(rectangleLength * rectangles.count, rectangleLength)];

// add scroll view to parent view
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320, rectangleLength)];
[containerView addSubview:_scrollView];


// compute CGPoint to center of first rectangle
CGPoint number1RectanglePoint = CGPointMake(0 * rectangleLength + 50, 50);

// compute CGPoint to center of fifth rectangle
CGPoint number5RectanglePoint = CGPointMake(4 * rectangleLength + 50, 50);

UIView *firstSubview = [containerView hitTest:number1RectanglePoint withEvent:nil];
UIView *fifthSubview = [containerView hitTest:number5RectanglePoint withEvent:nil];

if (firstSubview) NSLog(@"first rectangle OK");
if (fifthSubview) NSLog(@"fifth rectangle OK");

输出:第一个矩形OK

最佳答案

您应该能够遍历 ScrollView subview

+(UIView *)touchedViewIn:(UIScrollView *)scrollView atPoint:(CGPoint)touchPoint {

CGPoint actualPoint = CGPointMake(touchPoint.x + scrollView.contentOffset.x, scrollView.contentOffset.y + touchPoint.y);

for (UIView * subView in scrollView.subviews) {
if(CGRectContainsPoint(subView.frame, actualPoint)) {
NSLog(@"THIS IS THE ONE");
return subView;
}

}

//Nothing touched
return nil;
}

关于ios - 如何使用 CGPoint 获取 UIScrollView 的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26312619/

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