当uiscrollview框架触摸主屏幕边界时检测

时间:2011-05-22 14:32:15

标签: iphone uiscrollview scroll geometry

如何在上下两个方向检测UIScrollView帧与主屏幕(滚动时)相交的时间?我试图这样做:

- (void)detectScreenBoundsIntersect{
    if (CGRectIntersectsRect([myScrollView frame], [[UIScreen mainScreen] bounds])) {
        NSLog(@"COLLISION");
    }
} 

但这似乎不起作用!感谢。

1 个答案:

答案 0 :(得分:0)

在视图控制器中采用UIScrollViewDelegate协议,并将滚动视图的委托设置为控制器。采用scrollViewDidScroll:方法。

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    if ( [self hasReachedAVerticalEdge] ) {
        NSLog(@"At World's End");
    }
}

- (BOOL) hasReachedAVerticalEdge {
    CGPoint offset = myScrollView.contentOffSet;
    CGSize contentSize = myScrollView.contentSize;
    CGFloat height = myScrollView.frame.size.height;
    CGFloat width  = myScrollView.frame.size.width;

    if ( offset.y == 0 || 
         (offset.y + height) == contentSize.height ) {
        return YES;
    }

    return NO;
}

这是你在找什么?

检查图像视图是否属于滚动视图的可见部分。

- (BOOL)isContentFrameVisible:(CGRect)aFrame {
    CGRect visibleRect = CGRectZero;
    visibleRect.origin = myScrollView.offset;
    visibleRect.size = myScrollView.frame.size;

    if ( CGRectIntersectsRect(visibleRect, aFrame) ) {
        return YES;
    }
}