2020 Note

1、检测view是否在屏幕上是否可见

@implementation UIView (CheckVisible)
+ (BOOL)_checkVisible:(UIView *)view inView:(UIView *)inView
{
    if(!inView){
        return YES;
    }
    CGRect frameInView = [inView convertRect:view.frame fromView:view.superview];
//    NSLog(@"frameInView -> %@(%@) : %@",
//          NSStringFromClass(inView.class),
//          NSStringFromCGRect(inView.bounds),
//          NSStringFromCGRect(frameInView));
    if(CGRectIntersectsRect(frameInView, inView.bounds))
    {
        return [self _checkVisible:view inView:inView.superview];
    }
    return NO;
}

- (BOOL)checkVisibleOnScreen
{
    if(self.hidden){
        return NO;
    }
    if(!self.window){
        return NO;
    }
    if(!self.superview){
        return NO;
    }
    return [UIView _checkVisible:self inView:self.superview];
}

@end

CGRectIntersectsRect 检测两个rect是否相交
https://developer.apple.com/documentation/coregraphics/1454747-cgrectintersectsrect

推荐阅读更多精彩内容