CGRectIntersection函数与CGRectIsNull函数

CGRectIntersection函数可以返回一个CGRect类型的值以表示两个CGRect类型变量的交集部分,如果不存在相交部分则返回CGRectIsNull

可以通过CGRectIsNull函数判断一个CGRect类型的变量是否为CGRectNull

示例代码:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        CGRect intersectionRect = CGRectZero;

        CGRect r1 = CGRectMake(100, 100, 200, 200);
        CGRect r2 = CGRectMake(200, 200, 300, 400);
        intersectionRect = CGRectIntersection(r1, r2);
        NSLog(@"CGRectIntersection(r1, r2) = %@", NSStringFromRect(intersectionRect));

        CGRect r3 = CGRectMake(0, 0, 50, 50);
        CGRect r4 = CGRectMake(60, 60, 50, 50);
        intersectionRect = CGRectIntersection(r3, r4);
        NSLog(@"CGRectIntersection(r3, r4) = %@", NSStringFromRect(intersectionRect));
        
        // 可以通过CGRectIsNull函数判断一个CGRect类型的变量是否为CGRectNull
        NSLog(@"%@", CGRectIsNull(CGRectNull) ? @"YES" : @"NO");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xwoder/p/4463238.html