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
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
Swift
Use
CGRect.contains(_: CGPoint)
: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.
In Swift that would look like this:
Swift 3 version:
Link to documentation . Please remember to check containment if both are in the same coordinate system if not then conversions are required (some example)
UIView's pointInside:withEvent: could be a good solution. Will return a boolean value indicating wether or not the given CGPoint is in the UIView instance you are using. Example:
In swift you can do it like this:
"frame" is a CGRect and "point" is a CGPoint
In objective c you can use CGRectContainsPoint(yourview.frame, touchpoint)
In swift 3 yourview.frame.contains(touchpoint)
It is so simple,you can use following method to do this kind of work:-
In your case,you can pass imagView.center as point and another imagView.frame as rect in about method.
You can also use this method in bellow UITouch Method :
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:
It prints inside