UIViews are subviews of UIScrollView. How to trace that a subview is inside defined rectangle?
I add UIViews to UIScrollView one under another. When user scrolls and current UIView passes a rectangle it has to change background color of UIView. How to trace that a subview is inside defined rectangle?
- (v开发者_StackOverflowoid) 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);
}
}
The does CGRectIntersection();
might be what you are looking for.
It returns a CGRect
, the intersection, which can be null (check it with CGRectIsNull()
). If the rects have same size, and they are one over the second, the result should be a CGRect
with same size.
I've found the solution in delegate method and property 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];
}
}
精彩评论