Subtract CGRect from CGRect — largest piece of one not containing the other

后端 未结 3 1634
不思量自难忘°
不思量自难忘° 2021-02-07 08:05

How I can substract one CGRect from another? I want the result R1 - R2 to be the largest subrectangle of R1 that does not intersect R2.

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-07 08:55

    CGRect newRect = CGRectMake(0, 0, rect2.size.width - rect1.size.width, rect2.size.height - rect1.size.height);
    

    In response to your illustration, this code I've given you here will do exactly what you want (assuming you don't care about the origin XY coordinates). I've looked through the docs for CGGeometry functions, and there doesn't seem to be a CGRectDifference or other such method defined. There is, however, CGRectUnion, but that does the opposite of what you are looking for.

提交回复
热议问题