-4

I am using CGRectContainsPoint to find whether a point is inside a rect but it always returns false.

If I am wrong please correct me and help me understand whether the point is inside the CGRect. Here is my code:

    BOOL isObjectInside = false;

    CGPoint serverPoint = CGPointMake(78,157);

    CGRect frame = CGRectMake(31, 207, 98, 28);


    if(CGRectContainsPoint(frame, serverPoint))
    {
        isObjectInside = true;
    }
    else
    {
        isObjectInside = false;

    }
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
user3115014
  • 667
  • 1
  • 8
  • 23
  • 1
    the y position of your serverPoint is 157, but your frame starts only at 207. So it doesn't contain the point. Try giving 210 or something for the y position of the serverPoint. – Ramaraj T Feb 11 '15 at 07:00

1 Answers1

2

Here is a rendering of your view hierarchy:

enter image description here

Note that the red dot is not inside the yellow square.

Therefore, it's expected that CGRectContainsPoint would return NO / false.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • If I get (x,y) from server I have to find the object that contains the x,y by itreating through each view present in the window – user3115014 Feb 11 '15 at 06:55
  • Are you looking for `hitTest:withEvent:`? – Aaron Brager Feb 11 '15 at 06:59
  • `frame` doesn't contain `serverPoint`, so it's correct that `CGRectContainsPoint` returns `NO`. Anything else you didn't ask in your question. – Aaron Brager Feb 11 '15 at 07:00
  • no just finding the whether the objects present in a UIViewController have points which are passed from the server – user3115014 Feb 11 '15 at 07:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70683/discussion-between-user3115014-and-aaron-brager). – user3115014 Feb 11 '15 at 07:02
  • Didn't you [already ask that a week ago](https://stackoverflow.com/questions/28341696/find-the-object-from-x-y-in-ios)? Did you also read [How to get UIView given a CGPoint](http://stackoverflow.com/q/6033220/1445366)? – Aaron Brager Feb 11 '15 at 07:02
  • that wont work in my case as I am building a static library and I am unable to get reference to the base view .Instead it comes as UILayoutContainerView – user3115014 Feb 11 '15 at 07:04
  • 1
    You should post a new question with whatever your actual question is. – Aaron Brager Feb 11 '15 at 07:06