2011-10-09 26 views

Trả lời

166

Thực hiện UIScrollViewDelegate trong lớp học của bạn, và sau đó thêm này:

-(void)scrollViewDidScroll: (UIScrollView*)scrollView 
{ 
    float scrollViewHeight = scrollView.frame.size.height; 
    float scrollContentSizeHeight = scrollView.contentSize.height; 
    float scrollOffset = scrollView.contentOffset.y; 

    if (scrollOffset == 0) 
    { 
     // then we are at the top 
    } 
    else if (scrollOffset + scrollViewHeight == scrollContentSizeHeight) 
    { 
     // then we are at the end 
    } 
} 

Hy vọng điều này là những gì bạn đang theo đuổi! Người khác có tinker bằng cách thêm nhiều điều kiện hơn vào mã trên và NSLog giá trị của scrollOffset.

+3

Nếu bạn đang ở trong một 'navigationController', bạn có thể muốn làm một cái gì đó như thế này: 'scrollOffset <= (0 - self.navigationController.navigationBar.frame.size.height - 20) 'và' (scrollOffset + scrollHeight)> = scrollContentSizeHeight' –

+0

Vấn đề với điều đó là thư bị trả lại ... mọi thứ được gọi hai lần khi đạt đến đỉnh hoặc bot tom. Có một giải pháp cho điều đó, bên cạnh việc tắt bảng hoặc thu hồi xem bộ sưu tập? – denikov

+0

@denikov bạn có tìm thấy giải pháp nào cho điều đó không? – Priyal

59

Vâng, contentInsets cũng có liên quan, khi bạn cố gắng xác định xem scrollView có ở trên cùng hay ở dưới cùng. Bạn cũng có thể quan tâm đến các trường hợp khi scrollView của bạn là phía trên trên cùng và bên dưới phần dưới cùng. Đây là mã tôi sử dụng để tìm trên và dưới vị trí:

Swift:

extension UIScrollView { 

    var isAtTop: Bool { 
     return contentOffset.y <= verticalOffsetForTop 
    } 

    var isAtBottom: Bool { 
     return contentOffset.y >= verticalOffsetForBottom 
    } 

    var verticalOffsetForTop: CGFloat { 
     let topInset = contentInset.top 
     return -topInset 
    } 

    var verticalOffsetForBottom: CGFloat { 
     let scrollViewHeight = bounds.height 
     let scrollContentSizeHeight = contentSize.height 
     let bottomInset = contentInset.bottom 
     let scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight 
     return scrollViewBottomOffset 
    } 

} 

Objective-C:

@implementation UIScrollView (Additions) 

- (BOOL)isAtTop { 
    return (self.contentOffset.y <= [self verticalOffsetForTop]); 
} 

- (BOOL)isAtBottom { 
    return (self.contentOffset.y >= [self verticalOffsetForBottom]); 
} 

- (CGFloat)verticalOffsetForTop { 
    CGFloat topInset = self.contentInset.top; 
    return -topInset; 
} 

- (CGFloat)verticalOffsetForBottom { 
    CGFloat scrollViewHeight = self.bounds.size.height; 
    CGFloat scrollContentSizeHeight = self.contentSize.height; 
    CGFloat bottomInset = self.contentInset.bottom; 
    CGFloat scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight; 
    return scrollViewBottomOffset; 
} 


@end 
+1

tốt.Điều này là chính xác! câu trả lời trước là sai. –

+0

Câu trả lời hay này, việc triển khai scrollviewdidscroll là xấu đối với các hoạt động – Vassily

+2

Làm cách nào để gọi nó nếu không sử dụng scrollViewDidScroll? –

4

tôi đã tìm ra một cách chính xác làm thế nào để làm điều đó:

CGFloat maxPosition = scrollView.contentInset.top + scrollView.contentSize.height + scrollView.contentInset.bottom - scrollView.bounds.size.height; 
CGFloat currentPosition = scrollView.contentOffset.y + self.topLayoutGuide.length; 

if (currentPosition == maxPosition) { 
    // you're at the bottom! 
} 
14

Nếu bạn muốn mã nhanh chóng:

override func scrollViewDidScroll(scrollView: UIScrollView) { 

    if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) { 
     //reach bottom 
    } 

    if (scrollView.contentOffset.y <= 0){ 
     //reach top 
    } 

    if (scrollView.contentOffset.y > 0 && scrollView.contentOffset.y < (scrollView.contentSize.height - scrollView.frame.size.height)){ 
     //not top and not bottom 
    } 
} 
3

đơn giản và sạch sẽ mở rộng:

import UIKit 

extension UIScrollView { 

    var scrolledToTop: Bool { 
     let topEdge = 0 - contentInset.top 
     return contentOffset.y <= topEdge 
    } 

    var scrolledToBottom: Bool { 
     let bottomEdge = contentSize.height + contentInset.bottom - bounds.height 
     return contentOffset.y >= bottomEdge 
    } 

} 

On GitHub:

https://github.com/salutis/swift-scroll-view-edges

Các vấn đề liên quan