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.
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
.m