IOS: verify if a point is inside a rect

前端 未结 8 1417
夕颜
夕颜 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条回答
  •  -上瘾入骨i
    2021-01-30 05:16

    In Swift that would look like this:

    let point = CGPointMake(20,20)
    let someFrame = CGRectMake(10,10,100,100)
    let isPointInFrame = CGRectContainsPoint(someFrame, point)
    

    Swift 3 version:

    let point = CGPointMake(20,20)
    let someFrame = CGRectMake(10,10,100,100)
    let isPointInFrame = someFrame.contains(point)
    

    Link to documentation . Please remember to check containment if both are in the same coordinate system if not then conversions are required (some example)

提交回复
热议问题