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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:55:11+00:00 2026-06-11T00:55:11+00:00

Advaced Question: How can I get equivalent functionality across all iterations of a programmatically

  • 0

Advaced Question: How can I get equivalent functionality across all iterations of a programmatically created series of UILabel objects in UIView (to function across CGIntersect with a second series of UILabels)?

Can all instances of a programmatically created UILabel series possess the equivalent title (self.MYUILABEL) and retain equivalent functionality?

I made a series of equivalently classed UILabels programmatically with a for loop, but only the last made instance of UILabel is assigned the UILabel title.

How can I get equivalent functionality across all iterations of a programmatically created series of UILabel objects in UIView?

The goal is to get one series of UILabels to be moved by touch to a second series of UILabels.

Here is how I made the UILabels (which act as goal zones) in my view.

for (int i=0;i<characterCount ;i++){
    self.myBottomLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 200.0, 50.0, 50.0)];
    self.myBottomLabel.backgroundColor = [UIColor whiteColor];
    self.myBottomLabel.text= [myword objectAtIndex:i];
    self.myBottomLabel.userInteractionEnabled = NO;
    self.myBottomLabel.tag=300+[[letterToNumber objectForKey:[myword objectAtIndex:i]] integerValue];
    [self.view insertSubview: self.myBottomLabel atIndex:(500)];
}

When I attempt to use CGRectIntersectsRect using self.myBottomLabel only the last UILabel made will function correctly with the self.myBottomLabel title used by thisCGRect:

theReceivingCard = [self.myBottomLabel convertRect:[self.myBottomLabel frame] toView:self.view];

Here is almost all of the implementation. Do I need to create multiple CGRects (Which I don’t know how to do)? Or is there someway to find exactly what the tag is of these UILabel (which are made interactive when you move a corresponding UILabel over the top of them?)

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray* myword=[NSArray arrayWithObjects:@"h",@"e",@"l",@"l",@"o",nil];

    NSDictionary *letterToNumber;
    letterToNumber = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"0", @"a", 
                      @"1", @"b", 
                      @"2", @"c", 
                      @"3", @"d",
                      @"4", @"e", 
                      @"5", @"f", 
                      @"6", @"g", 
                      @"7", @"h", 
                      @"8", @"i", 
                      @"9", @"j", 
                      @"10", @"k", 
                      @"11", @"l", 
                      @"12", @"m", 
                      @"13", @"n", 
                      @"14", @"o", 
                      @"15", @"p", 
                      @"16", @"q", 
                      @"17", @"r", 
                      @"18", @"s", 
                      @"19", @"t", 
                      @"20", @"u", 
                      @"21", @"v", 
                      @"22", @"w", 
                      @"23", @"x", 
                      @"24", @"y", 
                      @"25", @"z", 
                      nil];    

NSUInteger characterCount = [myword count];

    //moveable
    for (int i=0;i<characterCount ;i++){
        self.myTopLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 100.0, 50.0, 50.0)];
        self.myTopLabel.backgroundColor = [UIColor whiteColor];
        self.myTopLabel.text= [myword objectAtIndex:i];
        self.myTopLabel.userInteractionEnabled = YES;
        self.myTopLabel.tag=100+[[letterToNumber objectForKey:[myword objectAtIndex:i]] integerValue];
        [self.view insertSubview: self.myTopLabel atIndex:(1)];
    }

    //receiver
    for (int i=0;i<characterCount ;i++){
        self.myBottomLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 200.0, 50.0, 50.0)];
        self.myBottomLabel.backgroundColor = [UIColor whiteColor];
        self.myBottomLabel.text= [myword objectAtIndex:i];
        self.myBottomLabel.userInteractionEnabled = NO;
        self.myBottomLabel.tag=300+[[letterToNumber objectForKey:[myword objectAtIndex:i]] integerValue];
        [self.view insertSubview: self.myBottomLabel atIndex:(500)];
    }

}

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

    [[viewYouWishToObtain superview] bringSubviewToFront:viewYouWishToObtain];

    if ([touch view] != viewYouWishToObtain && viewYouWishToObtain.tag >= 100 && viewYouWishToObtain.tag <= 200) {
        if ([touch tapCount] == 2) {
        }
        return;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];   
    CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
    UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];

    if ([touch view] == viewYouWishToObtain && viewYouWishToObtain.tag >= 100 && viewYouWishToObtain.tag <= 200) {
        CGPoint location = [touch locationInView:self.view];
        viewYouWishToObtain.center = location;      

        theMovingCard = [viewYouWishToObtain convertRect:[viewYouWishToObtain frame] toView:self.view];

        theReceivingCard =  [self.myBottomLabel convertRect:[self.myBottomLabel frame] toView:self.view];

        CGRect theZone = CGRectMake(theReceivingCard.origin.x, theReceivingCard.origin.y,theReceivingCard.size.width*2 , theReceivingCard.size.height*2);
        CGRect theCard = CGRectMake(theMovingCard.origin.x, theMovingCard.origin.y,theMovingCard.size.width*2 , theMovingCard.size.height*2);

            if(CGRectIntersectsRect(theCard, theZone))
            {
                NSLog(@"intersect");
            }
        return;
    }
}

The problem is that right there at the end of all the code, the intersect only functions on the last made UILabel. I need the intersect to work across all of the series of UILabels that act as receivers. Super super super super thanks in advance.

  • 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-11T00:55:13+00:00Added an answer on June 11, 2026 at 12:55 am

    Here is the answer of “how to programmatically create multiple CGRect values for multiple CGIntersects for UIView subset matching” by iteration through a for loop.

    This is the complete file.
    It is an iOS touch enabled foundation for a card game (like solitaire) or as match game (children’s education).

    The program makes 10 cards where the top row has to match the bottom row. If the top row card has text equivalent to the bottom row card then the CGIntersect will ‘hit’ and you can execute any additional methods.

    There should be a simpler way to make variable CGRect, but I can’t find the answer. If someone can figure out how to NOT use a NSMutableArray and a for-loop to generate the CGrect code, then I would be greatly obliged if you would share.

    .h

        #import <UIKit/UIKit.h>
    
        @interface ViewController : UIViewController
    
            @property NSString* currentCard;
            @property NSArray* myWord;
            @property NSString* myCurrentLetter;
            @property NSMutableArray* myMoverMutableArray;
            @property NSMutableArray* myReceiverMutableArray;
    
        @end
    

    .m

        #import "ViewController.h"
    
        @interface ViewController ()
        {
            int myLetterTagNumber;
        }
        @end
    
        @implementation ViewController
    
            @synthesize currentCard = _currentCard;
            @synthesize myWord = _myWord;
            @synthesize myCurrentLetter = _myCurrentLetter;
            @synthesize myMoverMutableArray = _myMoverMutableArray;
            @synthesize myReceiverMutableArray = _myReceiverMutableArray;
    
        - (void)viewDidLoad
        {
            [super viewDidLoad];
            [self writer];
        }
    
        -(void)writer{
    
            self.myWord=[NSArray arrayWithObjects:@"l",@"i",@"l",@"l",@"y",nil];
    
            NSUInteger characterCount = [self.myWord count];
    
            self.myMoverMutableArray = [NSMutableArray arrayWithCapacity:characterCount];
            self.myReceiverMutableArray = [NSMutableArray arrayWithCapacity:characterCount];
    
            //moveable
            for (int i=0;i<characterCount ;i++){
                UILabel*myTopLabel;
                myTopLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 100.0, 50.0, 50.0)];
                myTopLabel.backgroundColor = [UIColor whiteColor];
                myTopLabel.text= [self.myWord objectAtIndex:i];
                myTopLabel.userInteractionEnabled = YES;
                myTopLabel.textAlignment = UITextAlignmentCenter;
                myTopLabel.tag=100+i;
                [self.myMoverMutableArray addObject:[self.myWord objectAtIndex:i]];
                [self.view insertSubview: myTopLabel atIndex:(100)];
            }
    
            //receiver
            for (int i=0;i<characterCount ;i++){
                UILabel*myBottomLabel;
                myBottomLabel=[[UILabel alloc] initWithFrame: CGRectMake((i*60.0)+10, 200.0, 50.0, 50.0)];
                myBottomLabel.backgroundColor = [UIColor redColor];
                myBottomLabel.text= [self.myWord objectAtIndex:i];
                myBottomLabel.userInteractionEnabled = YES;
                myBottomLabel.textAlignment = UITextAlignmentCenter;
                myBottomLabel.tag=300+i;
                [self.myReceiverMutableArray addObject:[self.myWord objectAtIndex:i]];        
                [self.view insertSubview: myBottomLabel atIndex:(300)];
            }
        }
    
        - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
            UITouch *touch = [touches anyObject];
            CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
            UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];
    
            //clipping
            [[viewYouWishToObtain superview] bringSubviewToFront:viewYouWishToObtain];
    
            myLetterTagNumber = viewYouWishToObtain.tag;
            UILabel * myTempUILabel = (UILabel*)[self.view viewWithTag:(myLetterTagNumber)];
            self.myCurrentLetter = myTempUILabel.text;
    
            NSLog (@"text: %@",myTempUILabel.text);
            NSLog (@"Tag: %d",myLetterTagNumber);
    
            if ([touch view] != viewYouWishToObtain && viewYouWishToObtain.tag >= 100 && viewYouWishToObtain.tag <= 200) {
                if ([touch tapCount] == 2) {
                }
                return;
            }
        }
    
        - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
            UITouch *touch = [touches anyObject];
    
            CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
            UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];
    
            if ([touch view] == viewYouWishToObtain && viewYouWishToObtain.tag >= 100 && viewYouWishToObtain.tag <= 200) {
                CGPoint location = [touch locationInView:self.view];
                viewYouWishToObtain.center = location;
    
                //moving card rec
                CGRect theMovingCard;
                theMovingCard = [viewYouWishToObtain convertRect:[viewYouWishToObtain frame] toView:self.view];
                CGRect themovingRect = CGRectMake(theMovingCard.origin.x, theMovingCard.origin.y,theMovingCard.size.width*2 , theMovingCard.size.height*2);
    
                NSString *myTempString;
                NSUInteger characterCount = [self.myWord count];
    
                for (int i=0;i<characterCount ;i++){
    
                    myTempString= [self.myReceiverMutableArray objectAtIndex:i];
    
                    if (myTempString == self.myCurrentLetter){
                        UILabel * mytouchMoveUILabel = (UILabel*)[self.view viewWithTag:(i+300)];
                        CGRect theReceivingCard;
                        theReceivingCard =  [mytouchMoveUILabel convertRect:[mytouchMoveUILabel frame] toView:self.view];
    
                        CGRect spaceToHitRect = CGRectMake(theReceivingCard.origin.x, theReceivingCard.origin.y,theReceivingCard.size.width*2 , theReceivingCard.size.height*2);
                        if(CGRectIntersectsRect(themovingRect, spaceToHitRect))
                        {
                            NSLog (@"Intersect: %d",myLetterTagNumber);
                        }
                    }//if
                }//for
                return;
            }
        }
    
        - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
            UITouch *touch = [touches anyObject];
            // NSLog(@"3");
    
            CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
            UIView* viewYouWishToObtain = [self.view hitTest:locationPoint withEvent:event];
    
            if ([touch view] == viewYouWishToObtain && viewYouWishToObtain.tag >= 100 && viewYouWishToObtain.tag <= 200) {
                return;
            }
        }
    
                    - (void)viewDidUnload
                    {
                        [super viewDidUnload];
                        // Release any retained subviews of the main view.
                    }
    
                    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
                    {
                        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
                            return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
                        } else {
                            return YES;
                        }
                    }
    
        @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about localization how can i do localization lets say that
I think the title of the question says it all. I am developing a
public class Question { public int Id { get; set; } public int? Score
I know, that this question was created many times, but it is stil open
If you using Facebook graph API, you can see all public information of someone
All, I'm pretty new to AS3, so this is probably a very trivial question.
Slightly more advanced mapping then in my previous question :) Tables: create table [Primary]
This is a bit confused question for those who don't now advanced SQL... However.
I recently created a advanced form with elements that use jquery's $().hide & $().show
I have a question regarding a rather advanced DataModel which I would like to

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.