[ACCEPTED]-How to test if a point is in a view-cgpoint

Accepted answer
Score: 51

CGPoint is no good with a reference point. If your 2 point is in window's coordinates then you 1 can get it using

CGPoint locationInView = [imageView convertPoint:point fromView:imageView.window];
if ( CGRectContainsPoint(imageView.bounds, locationInView) ) {
    // Point lies inside the bounds.
}

You may also call pointInside:withEvent: method

if ( [imageView pointInside:locationInView withEvent:nil] ) {
    // Point lies inside the bounds
}
Score: 15

Tested in Swift 4

view.frame.contains(point)

0

Score: 2
if(CGRectContainsPoint([myView frame], point))

where point is your CGPoint and myView is 1 your UIImageView

Score: 2

I'll assume you have a full-screen window 5 (pretty reasonable, I think). Then you can 4 transform the point from the window's coordinate 3 space to the UIImageView's using:

CGPoint point = ...
UIWindow window = ...
UIImageView imageView = ...
CGPoint transformedPoint = [window convertPoint:point toView:imageView];

Then, you 2 can test if the point is in the image view's 1 frame as follows:

if(CGRectContainsPoint(imageView.frame, transformedPoint))
{
    // do something interesting....
}
Score: 0

In Swift 3

let isPointInFrame = UIScreen.main.bounds.contains(newLocation)

0

More Related questions