iosuiviewcollision-detectioncgrectcgpoint

iOS: verify if a point is inside a rect


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.


Solution

  • Swift 4

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

    Objective-C

    Use CGRectContainsPoint():

    bool CGRectContainsPoint(CGRect rect, CGPoint point);

    Parameters

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