@interface
-(BOOL) Isdisplayedinscreen;
@end
@implementation
UIView (uiscreendisplaying)
Determine if the view is displayed on the screen
-(BOOL) isdisplayedinscreen{
if (self = = nil) {
return FALSE;
}
CGRect screenrect = [UIScreen mainscreen].bounds;
Convert view to Rect for window
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 there is no Superview
if (Self.superview = = nil) {
return false;
}
If size is Cgrectzero
if (Cgsizeequaltosize (Rect.size, Cgsizezero)) {
return false;
}
Gets the rect that the view crosses with the window
CGRect intersectionrect = cgrectintersection (rect, screenrect);
if (Cgrectisempty (intersectionrect) | | Cgrectisnull (Intersectionrect)) {
return false;
}
Retrun true;
}
@end
Test case
UIView *view = [UIView alloc] Initwithframe:cgrectmake (0,0,40,40)];
No parent view
BOOL B1 = [view Isdisplayedinscreen];
NSLog (@ "B1:%d", B1);
[Self.view Addsubview:view];
BOOL B2 = [view Isdisplayedinscreen];
NSLog (@ "b2:%d", B2);
V.frame = Cgrectzero;
BOOL B3 = [view Isdisplayedsceen];
NSLog ("b3:%d", B3);
CGRect screenrect = [UIScreen mainscreen].bounds;
CGFloat screenwidth = screenRect.size.width;
CGFloat screenheight = screenRect.size.height;
View.frame = CGRectMake (-screenwidth,-screenheight, ScreenWidth, screenheight);
BOOL b4 = [view Isdisplayedinscreen];
NSLog (@ "b4:%d", B4);
iOS determines if UIView is displayed on the screen