【问题标题】:UIViews are subviews of UIScrollView. How to trace that a subview is inside defined rectangle?UIViews 是 UIScrollView 的子视图。如何跟踪子视图在定义的矩形内?
【发布时间】:2011-07-16 19:19:36
【问题描述】:

我将 UIViews 添加到 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);
     }


}

【问题讨论】:

  • 抱歉,请问您有什么问题?
  • @PengOne 抱歉,问题在标题中。如何在定义的矩形内跟踪子视图?
  • 滚动停止时或滚动时是否需要更改颜色?
  • @Vince 我需要在滚动时更改它。当内部 intersectRect 颜色为蓝色时,外部颜色为绿色。只是为了简化。

标签: ios xcode uiscrollview


【解决方案1】:

CGRectIntersection(); 可能就是你 looking 的用途。

它返回一个CGRect,即交集,可以为空(与CGRectIsNull() 一起检查)。如果矩形具有相同的大小,并且它们是一秒多一,则结果应该是具有相同大小的CGRect

【讨论】:

  • 我知道函数,但我不明白要比较哪些帧,因为视图相对于 UIScrollView 定位。
  • 是的,我在阅读您@PengOne 的评论后做出了这个回答,滚动视图代表不能处理这个问题吗?
  • 我只是查看了委托方法,但找不到合适的方法。简单的任务,但还不知道:)
【解决方案2】:

我在委托方法和属性 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];

    }

}

【讨论】:

    猜你喜欢
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多