Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8238603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:52:16+00:00 2026-06-07T19:52:16+00:00

I have a layout for an application that needs to detect when an image

  • 0

I have a layout for an application that needs to detect when an image collides with another image.

Here, the user creates multiple ‘balls’, the same instance of a UIImageView named ‘imgView’, on the screen by tapping the location they desire:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *myTouch = [[event allTouches] anyObject];
    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
    imgView.image = [UIImage imageNamed:@"ball.png"];
    [self.view addSubview:imgView];
    imgView.center = [myTouch locationInView:self.view];

}

(imgView is declared as a UIImageView in the header) :

    UIImageView *imgView;

Now, I also have an image called ‘staff’. It is a long bar that pans horizontally across the screen. I want the image ‘staff’ to detect EVERY collision it has with the variable ‘imgView’, or the balls the user has placed on the screen.
Therefore, the user could tap 10 different places on the screen and and ‘staff’ should be able to catch every single one.

I use this CGRectIntersectsRect code thats activated by an NSTimer:

-(void)checkCollision {
    if( CGRectIntersectsRect(staff.frame,imgView.frame)) {
    [self playSound];
    }
}

However, the intersection is only detected with the LAST instance, or ‘ball’ the user created. The staff reacts to that one, but pans over the rest. Any help to fix my code to detect all instances would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:52:17+00:00Added an answer on June 7, 2026 at 7:52 pm

    Each time you create a new ball, you are overriding your imgView instance variable. Thus, your checkCollision method only sees the latest value of imgView, namely the last ball created.

    Instead, you could keep track of every ball on screen in an NSArray, then check for collisions against every element in that array. To do that, replace your imgView instance variable with, say:

    NSMutableArray *imgViews
    

    Then, sometime early on, say in viewDidLoad initialize the array:

     imgViews = [[NSMutableArray alloc] init]
    

    In -touchesEnded:withEvent: add the new UIImageView to the array:

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
         UITouch *myTouch = [[event allTouches] anyObject];
         UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
         imgView.image = [UIImage imageNamed:@"ball.png"];
         [self.view addSubview:imgView];
         imgView.center = [myTouch locationInView:self.view];
         [imgViews addObject:imgView]
    }
    

    And finally, in checkCollision iterate through your array and perform your check on every element

     - (void)checkCollision {
          for (UIImageView *imgView in imgViews) {
               if( CGRectIntersectsRect(staff.frame,imgView.frame)) {
                     [self playSound];
          }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have some basic user information that needs to be displayed
In my CakePHP application layout I have a section for latest posts that needs
I'm writing a WPF desktop application and have a view that needs to look
I have a muulti-language Access application that needs access to languages/keyboard layouts installed on
I have a listview that needs to be multiple choice (i.e each list item
I have a working zend mvc application that is using a layout, and this
I have an application layout in my rails app to give a default header/footer
I have implemented an application for modify the xml layout with some properties. I
I have some text views in an linear layout in my android application. Next
I have two libraries and a single application assembly project layout and I'm wondering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.