Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if UIView is in UIScrollView visible state

What is the easiest and most elegant way to check if a UIView is visible on the current UIScrollView's contentView? There are two ways to do this, one is involving the contentOffset.y position of the UIScrollView and the other way is to convert the rect area?

like image 914
adit Avatar asked Jun 04 '12 19:06

adit


3 Answers

If you're trying to work out if a view has been scrolled on screen, try this:

    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
    }
like image 196
Marc Avatar answered Nov 10 '22 16:11

Marc


Swift 5: in case that you want to trigger an event that checks that the entire UIView is visible in the scroll view:

extension ViewController: UIScrollViewDelegate {

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.bounds.contains(targetView.frame) {
            // entire UIView is visible in scroll view
        }
    }

}
like image 25
José Avatar answered Nov 10 '22 16:11

José


Implement scrollViewDidScroll: in your scroll view delegate and calculate manually which views are visible (e.g. by checking if CGRectIntersectsRect(scrollView.bounds, subview.frame) returns true.

like image 8
Jitendra Singh Avatar answered Nov 10 '22 17:11

Jitendra Singh