我将UIVcs添加到另一个UIScrollView。当用户滚动并且当前UIView通过矩形时,它必须改变UIView的背景颜色。 如何跟踪子视图是否在定义的矩形内?
- (void) showViews {
CGRect scrollRect = CGRectMake(0.0f, 44.0f, 704.0f, 704.0f);
UIScrollView *aScroll = [[UIScrollView alloc] initWithFrame:scrollRect];
float yPos = 20.0f;
//the rectangle to trace intersection
CGRect intersectRect = CGRectMake(0.0f, 200.0f, 704.0f, 400.0f);
for (UIView* view in arrayOfViews) {
CGRect viewRect = CGRectMake(100.0f, yPos, 320.0f, 480.0f);
view.frame = viewRect;
[aScroll addSubView:view];
yPos = yPos + 340.0f;
aScroll.contentSize = CGSizeMake(704.0, yPos);
}
}
答案 0 :(得分:1)
CGRectIntersection();
可能就是looking。{/ p>
它返回一个CGRect
,交集,可以为null(用CGRectIsNull()
检查)。如果rects具有相同的大小,并且它们是第二个,则结果应为CGRect
,且大小相同。
答案 1 :(得分:0)
我在委托方法和属性contentOffset中找到了解决方案。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
for (UIView *view in [[scrollView subviews] copy]) {
if ((view.frame.origin.y - scrollView.contentOffset.y - self.controllStrip.frame.origin.y - self.controllStrip.frame.size.height) < 0 &
( view.frame.origin.y + view.frame.size.height - scrollView.contentOffset.y - self.controllStrip.frame.origin.y) > 0) {
view.backgroundColor = [UIColor grayColor];
}
else
view.backgroundColor = [UIColor greenColor];
}
}