2012-06-04 36 views
14

Cách dễ nhất và thanh lịch nhất để kiểm tra xem UIView có hiển thị trên contentView của UIScrollView hiện tại không? Có hai cách để làm điều này, một là liên quan đến vị trí contentOffset.y của UIScrollView và cách khác là để chuyển đổi các khu vực rect?kiểm tra xem UIView có ở trạng thái hiển thị UIScrollView

Trả lời

1

Tôi nghĩ ý tưởng của bạn là chính xác. nếu nó là tôi tôi sẽ làm điều đó như sau:

//scrollView is the main scroll view 
//mainview is scrollview.superview 
//view is the view inside the scroll view 

CGRect viewRect = view.frame; 
CGRect mainRect = mainView.frame; 

if(CGRectIntersectsRect(mainRect, viewRect)) 
{ 
    //view is visible 
} 
5

Thực hiện scrollViewDidScroll: theo quan điểm của đại biểu cuộn của bạn và tính toán bằng tay mà quan điểm có thể nhìn thấy (ví dụ như bằng cách kiểm tra nếu CGRectIntersectsRect(scrollView.bounds, subview.frame) trả về true

15

Nếu bạn đang cố gắng. để làm việc ra nếu một cái nhìn đã được cuộn trên màn hình, hãy thử này:

CGRect thePosition = myView.frame; 
    CGRect container = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.frame.size.width, scrollView.frame.size.height); 
    if(CGRectIntersectsRect(thePosition, container)) 
    { 
     // This view has been scrolled on screen 
    } 
0

cập nhật cho nhanh 3

var rect1: CGRect! 
// initialize rect1 to the relevant subview 
if rect1.frame.intersects(CGRect(origin: scrollView.contentOffset, size: scrollView.contentSize)) { 
     // the view is visible 
    } 
Các vấn đề liên quan