Search code examples
iphoneobjectlocationuitouch

Possible to check number of objects in touch location?


Is it possible to check number of objects in a certain touch location? I have tagged all objects with a number, but couldn't think of a way it would work.

Basically what I want to do is add uiimageview to touch point, BUT when there is already an other uiimageview, I would do nothing.

Thanks!


Solution

  • You've got a good idea to start. All the objects that you add on a view are already kept in an array called [myview subviews]. It was a good idea to tag them because then you can easily access them with [myview viewWithTag: kFirstViewTag].

    So to answer the second part, when your checking touch locations, check if the touch location intersects with any of your subviews.

    For example:

    for (UIView* view in [myView subviews]) {
       if (CGRectContainsPoint([view frame], touchPoint) {
           //do something
       }
    }
    

    I can assume you probably don't need to go trough all the subviews, so you can just cycle to ones limited with tags from kFirstViewTag to kLastViewTag with for loop, like:

    for (int i = kFirstViewTag; i <= kLastViewTag; i++) {
       UIView *view = [myView viewWithTag: i];
       if (CGRectContainsPoint([view frame], touchPoint) {
           //do something
       }
    }