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 );