IOS: verify if a point is inside a rect

前端 未结 8 1413
夕颜
夕颜 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:13
    - (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");
        }
    
    0 讨论(0)
  • 2021-01-30 05:15

    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{
    
        }
    
    }
    
    0 讨论(0)
  • 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)

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

    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
    {
    }
    
    0 讨论(0)
  • 2021-01-30 05:31

    In swift you can do it like this:

    let isPointInFrame = frame.contains(point)
    

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

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

    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];
    
    0 讨论(0)
提交回复
热议问题