ios - CGRect intersects 4 rects. How to figure out which rect it intersects mostly? -
if need more details: have uicollectionviewcell draggable. when cell overlaps other cells during dragging want know 1 overlaps in order replace overlapped cell cell. there efficient way this? found nothing helpful in cggeometry. thank you.
you can use cgrectunion() in loop, 1 smallest area 1 greatest overlap. write function deals (and returns a) cgrects, youd need loop though views (cells) again find correct one, i'd keep uiview level... eg
//helpers cgfloat cgrectgetarea(cgrect therect){ return therect.size.width * therect.size.height; } -(uiview *)viewclosesttoview:(uiview*)keyview fromviews:(nsarray*)comparisonviews{ uiview *result=nil; cgfloat smallestarea=0.0; (uiview *view in comparisonviews){ cgfloat area = cgrectgetarea( cgrectunion(view.frame, keyview.frame) ); if ((area < smallestarea)|| ((nsinteger)smallestarea==0) ){ smallestarea=area; result=view; } } return result; }
Comments
Post a Comment