9

Tôi có một collectionView. Tôi muốn phát hiện hướng cuộn. Tôi có hai kiểu hoạt ảnh khác nhau để cuộn xuống và cuộn lên. Vì vậy, tôi phải học hướng cuộn.ios UICollectionView phát hiện hướng cuộn

CGPoint scrollVelocity = [self.collectionView.panGestureRecognizer 
velocityInView:self.collectionView.superview]; 

if (scrollVelocity.y > 0.0f) 
NSLog(@"scroll up"); 

else if(scrollVelocity.y < 0.0f)  
NSLog(@"scroll down"); 

Đây chỉ là công việc khi chạm ngón tay. Không làm việc cho tôi

Trả lời

26

Hãy thử điều này:

Thêm một nơi nào đó trong bạn tiêu đề:

@property (nonatomic) CGFloat lastContentOffset; 

Sau đó ghi đè lên scrollViewDidScroll: phương pháp:

#pragma mark - UIScrollViewDelegate 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if (self.lastContentOffset > scrollView.contentOffset.y) 
    { 
     NSLog(@"Scrolling Up"); 
    } 
    else if (self.lastContentOffset < scrollView.contentOffset.y) 
    { 
     NSLog(@"Scrolling Down"); 
    } 

    self.lastContentOffset = scrollView.contentOffset.y; 
} 

Tìm thấy trong Finding the direction of scrolling in a UIScrollView?

+0

Đây là công trình Cảm ơn bạn –

2

tôi đã cố gắng tìm cách phát hiện nếu người dùng chủ yếu là cố gắng kéo theo chiều dọc hoặc chiều ngang scrollView. Tôi cung cấp cho bạn giải pháp của tôi, tôi hy vọng nó có thể hữu ích cho bất kỳ ai:

CGPoint _lastContentOffset; 

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 
    _lastContentOffset = scrollView.contentOffset; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 

    if (ABS(_lastContentOffset.x - scrollView.contentOffset.x) < ABS(_lastContentOffset.y - scrollView.contentOffset.y)) { 
     NSLog(@"Scrolled Vertically"); 
    } else { 
     NSLog(@"Scrolled Horizontally"); 
    } 

} 

Tác phẩm này tìm tôi và tôi sử dụng để tránh scrollView di chuyển theo chiều ngang khi cuộn theo chiều dọc và ngược lại.

4

đây là cách tốt nhất để có hướng di chuyển, hy vọng điều này sẽ giúp bạn

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { 

    CGPoint targetPoint = *targetContentOffset; 
    CGPoint currentPoint = scrollView.contentOffset; 

    if (targetPoint.y > currentPoint.y) { 
     NSLog(@"up"); 
    } 
    else { 
     NSLog(@"down"); 
    } 
} 
Các vấn đề liên quan