IOS checks whether the UIView is displayed on the screen, and ios checks the uiview screen.

Source: Internet
Author: User

IOS checks whether the UIView is displayed on the screen, and ios checks the uiview screen.

Reprinted please indicate the source: column of ALDRIDGE1

Header file:

/*************************************** * *************** MIT Licence ** Author: _ Cheng _ Ying _ ** Date: 2015.2.10 *************************************** * ************/@ interface UIView (UIScreenDisplaying) // determine whether the View is displayed on the screen-(BOOL) isDisplayedInScreen; @ end

Implementation File

/*************************************** * *************** MIT Licence ** Author: _ Cheng _ Ying _ ** Date: 2015.2.10 *************************************** * ************/@ implementation UIView (UIScreenDisplaying) // determine whether the View is displayed on the screen-(BOOL) isDisplayedInScreen {if (self = nil) {return FALSE;} CGRect screenRect = [UIScreen mainScreen]. bounds; // convert the Rect CGRect rect = [self convertRect: self. frame fromView: nil]; if (CGRectIsEmpty (rect) | CGRectIsNull (rect) {return FALSE;} // if view is hidden if (self. hidden) {return FALSE;} // if no superview if (self. superview = nil) {return FALSE;} // if the size is CGrectZero if (CGSizeEqualToSize (rect. size, CGSizeZero) {return FALSE;} // obtain the Rect CGRect intersectionRect = CGRectIntersection (rect, screenRect) between the view and window; if (CGRectIsEmpty (intersectionRect) | CGRectIsNull (intersectionRect) {return FALSE;} return TRUE ;}

Test cases:

UIView * v = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 40, 40)]; // No parent view BOOL b1 = [v isDisplayedInScreen]; NSLog (@ "b1: % d", b1); // [self. view addSubview: v]; BOOL b2 = [v isDisplayedInScreen]; NSLog (@ "b2: % d", b2); v. frame = CGRectZero; BOOL b3 = [v isDisplayedInScreen]; NSLog (@ "b3: % d", b3); CGRect screenRect = [UIScreen mainScreen]. bounds; CGFloat screenWidth = screenRect. size. width; CGFloat screenHeight = screenRect. size. height; v. frame = CGRectMake (-screenWidth,-screenHeight, screenWidth, screenHeight); BOOL b4 = [v isDisplayedInScreen]; NSLog (@ "b4: % d", b4 );



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.