iOS: verify if a point is inside a rect
Asked Answered
P

8

160

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.

Paddle answered 7/11, 2011 at 14:19 Comment(0)
A
338

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.

Ahrens answered 7/11, 2011 at 14:23 Comment(3)
The missing link ;) developer.apple.com/library/mac/#documentation/graphicsimaging/…Obscenity
Is is possible to check CGPoint is in line context (CGContext)?Epifocal
in Swift 3.0 use like: rect = frame of a view, and then isContain = rect.contains(point)Mangrove
F
45

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)

Flat answered 29/10, 2015 at 9:3 Comment(0)
B
15

In swift you can do it like this:

let isPointInFrame = frame.contains(point)

"frame" is a CGRect and "point" is a CGPoint

Brandon answered 26/10, 2016 at 21:36 Comment(0)
U
12

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:

UIView *aView = [UIView alloc]initWithFrame:CGRectMake(0,0,100,100);
CGPoint aPoint = CGPointMake(5,5);
BOOL isPointInsideView = [aView pointInside:aPoint withEvent:nil];
Unpromising answered 7/11, 2011 at 14:24 Comment(0)
M
5

In objective c you can use CGRectContainsPoint(yourview.frame, touchpoint)

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch* touch = [touches anyObject];
CGPoint touchpoint = [touch locationInView:self.view];
if( CGRectContainsPoint(yourview.frame, touchpoint) ) {

}else{

}}

In swift 3 yourview.frame.contains(touchpoint)

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch:UITouch = touches.first!
    let touchpoint:CGPoint = touch.location(in: self.view)
    if wheel.frame.contains(touchpoint)  {

    }else{

    }

}
Marty answered 10/2, 2017 at 20:57 Comment(0)
W
3

It is so simple,you can use following method to do this kind of work:-

-(BOOL)isPoint:(CGPoint)point insideOfRect:(CGRect)rect
{
    if ( CGRectContainsPoint(rect,point))
        return  YES;// inside
    else
        return  NO;// outside
}

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 :

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
Whenas answered 6/7, 2015 at 10:19 Comment(0)
R
1

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

Recitative answered 29/4, 2018 at 9:58 Comment(0)
S
1
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
            UITouch *touch = [[event allTouches] anyObject];
            CGPoint touchLocation = [touch locationInView:self.view];
            CGRect rect1 = CGRectMake(vwTable.frame.origin.x, 
            vwTable.frame.origin.y, vwTable.frame.size.width, 
            vwTable.frame.size.height);
            if (CGRectContainsPoint(rect1,touchLocation))
            NSLog(@"Inside");
            else
            NSLog(@"Outside");
    }
Shulman answered 6/7, 2018 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.