iOS determines if UIView is displayed on the screen

Source: Internet
Author: User

@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

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.