UItableView 및 UIScrollView 스크롤 방향 판단

1501 단어
1. 먼저 UItableView에 관찰자 추가
[tableView addObserver: self forKeyPath: @"contentOffset" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: nil];

2. KVO에서 UItableView 대상의 관련 속성에 따라 UItableView 스크롤 방향의 판단을 완성한다.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([object isKindOfClass: [UITableView class]]) {
        CGPoint newPoint = [change[@"new"] CGPointValue];
        CGPoint oldPoint = [change[@"old"] CGPointValue];
        CGFloat newPointY = newPoint.y;
        CGFloat oldPointY = oldPoint.y;
        CGFloat frameHeight = ((UITableView *)object).frame.size.height;
        CGFloat contentSizeHeight = ((UITableView *)object).contentSize.height;

        //   contentSizeHeight long , CGFloat , 
        //  , , long int, 
        if ((newPointY < oldPointY && (frameHeight + newPointY) < (long)contentSizeHeight) || newPointY < 0 || oldPointY < 0) {
            if (newPointY <= 0 || oldPointY <= 0) {
                NSLog(@"top");
            } else {
                NSLog(@"scroll down");
            }
        } else if (newPointY > oldPointY) {
            if ((frameHeight + newPointY >= contentSizeHeight)) {
                NSLog(@"bottom");
            } else {
                NSLog(@"scroll up");
            }
        }
    }
}

좋은 웹페이지 즐겨찾기