IOS: verify if a point is inside a rect

前端 未结 8 1414
夕颜
夕颜 2021-01-30 04:55

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 t

相关标签:
8条回答
  • 2021-01-30 05:34

    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.

    0 讨论(0)
  • 2021-01-30 05:36

    I'm starting to learn how to code with Swift and was trying to solve this too, this is what I came up with on Swift's playground:

    // Code
    var x = 1
    var y = 2
    var lowX = 1
    var lowY = 1
    var highX = 3
    var highY = 3
    
    
    if (x, y) >= (lowX, lowY) && (x, y) <= (highX, highY ) {
        print("inside")
    } else {
        print("not inside")
    }
    

    It prints inside

    0 讨论(0)
提交回复
热议问题