IOS: verify if a point is inside a rect

cyclingIsBetter picture cyclingIsBetter · Nov 7, 2011 · Viewed 102.6k times · Source

Is there a way to verify if a CGPoint is inside a specific CGRect.

An example would be: I'm dragging a UIImageView and I want to verify if its central point CGPoint is inside another UIImageView

Answer

Ole Begemann picture Ole Begemann · Nov 7, 2011

Swift 4

let view = ...
let point = ...
view.bounds.contains(point)

Objective-C

Use CGRectContainsPoint():

bool CGRectContainsPoint(CGRect rect, CGPoint point);

Parameters

  • rect The rectangle to examine.
  • point The point to examine. Return Value true if the rectangle is not null or empty and the point is located within the rectangle; otherwise, false.

A point is considered inside the rectangle if its coordinates lie inside the rectangle or on the minimum X or minimum Y edge.