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 7931583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:45:44+00:00 2026-06-03T20:45:44+00:00

I am programmatically generating several UIButtons and then animating them with a block animation.

  • 0

I am programmatically generating several UIButtons and then animating them with a block animation. I am able to determine which button was touched by implementing the code in this answer (demonstrated below).

My issue now is that the images can overlap, so when there is more than 1 view at the given touch location, my code in touchesBegan pulls out the wrong button (i.e., gets the image underneath the visible button that I’m touching).

I wanted to use [touch view] to compare to the UIButtons on screen:

if (myButton==[touch view]) { ...

But that comparison always fails.

My touchesBegan:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    for (UIButton *brain in activeBrains) {
        //Works, but only when buttons do not overlap
        if ([brain.layer.presentationLayer hitTest:touchLocation]) {
            [self brainExplodes:brain];
            break;
        }

        /* Comparison always fails
        if (brain == [touch view]) {
            [self brainExplodes:brain];
            break;
        }
        */
    }
}

So my question is how can I determine which of the overlapping images is above the other(s)?

  • 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-03T20:45:45+00:00Added an answer on June 3, 2026 at 8:45 pm

    I made a few assumptions here in my code here, but essentially you need to get a list of all the Buttons that have been touched and then find the one ‘on top’. The one on top should have the highest index of the buttons in the array of subviews.

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInView:self.view];
        NSMutableArray *brainsTouched = [[NSMutableArray alloc] init];
        for (UIButton *brain in activeBrains) {
            //Works, but only when buttons do not overlap
            if ([brain.layer.presentationLayer hitTest:touchLocation]) {
                [brainsTouched addObject:brain];
            }
        }
        NSUInteger currentIndex;
        NSInteger viewDepth = -1;
        UIButton *brainOnTop;
        for (UIButton *brain in brainsTouched){
            currentIndex = [self.view.subviews indexOfObject:brain];
            if (viewDepth < currentIndex){ 
                brainOnTop = brain;
                viewDepth = currentIndex;
            }
        }
        [self brainExplodes:brainOnTop];
    }
    

    Also, I typed this in the edit window so please excuse typos.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am generating a webview programmatically I would like to add a button over
I am generating audio programmatically. I hear gaps of silence between my buffers. When
How does one obtain programmatically a reference to the object of which a FieldInfo
I'm trying to figure out a way to programmatically determine a printers inputslot tray
I am looking for a .NET library for programmatically generating tables, stored procedures, views
I am programatically generating a SQL statement that I then need to execute, however
I've been working on a process for programmatically generating Visual Studio projects using GetProjectTemplate.
I want to create layout programmatically in different class which is not an activity
I'm running into an issue with Wicket generating markup for elements that programmatically need
Programmatically, which is better for SEO purposes: defining web page titles client side (e.g.,

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.