How do you check if a UIImageView in an NSMutableDictionary Intersects with another UIImageView in same NSMutableDictionary?
I have 5 UIImageViews for each letter of the alphabet stored in an NSMutableDictionary. 5 instead of just 1, just in case a word they want to spell calls for more of the same letter.
I'm creating the letters like this... http://pastie.org/2510574
for (int i = 1; i <= 5; i++)
{
char a = 'a';
NSString *key = [NSString stringWithFormat:@"%c%d", a, i];
alphabetVowelA = [[UIImageView alloc] initWithFrame:CGRectMake(39, 104, 70, 70)];
[alphabetVowelA setImage:[UIImage imageNamed:@"a.png"]];
alphabetVowelA.tag = i;
[alphabetVowelA setUserInteractionEnabled:YES];
[self addGestureRecognizersToPiece:alphabetVowelA];
[letterDictionary setObject:alphabetVowelA forKey:key];
[self.view addSubview:alphabetVowelA];
[alphabetVowelA release];
}
for (int i = 1; i <= 5; i++)
{
char b = 'b';
NSString *key = [NSString stringWithFormat:@"%c%d", b, i];
alphabetB = [[UIImageView alloc] initWithFrame:CGRectMake(112, 104, 70, 70)];
[alphabetB setImage:[UIImage imageNamed:@"b.png"]];
alphabetB.tag = i;
[alphabetB setUserInteractionEnabled:YES];
[self addGestureRecognizersToPiece:alphabetB];
[letterDictionary setObject:alphabetB forKey:key];
[self.view addSubview:alphabetB];
[alphabetB release];
}
With the code below, I'm trying to check to see if one of the letters intersects with another from the same dictionary. However, with my code, when I pan/drag one of the letter tiles then drop it, it prints an NSLog message for every key in the dictionary. Here is a sample of its output.
http://pastie.org/2511092
This output happens, even though none of the UIImageViews within the dictionary intersected. 开发者_如何学PythonIt actually happens after panning then dropping the first letter tile, before even picking up a second one for it to intersect with on drop. I can't figure out why. My pan: code is below.
http://pastie.org/2511054
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];
[self.view bringSubviewToFront:piece];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGRect startingPointFrame = CGRectMake(245, 428, 31, 20);
[startingPoint setFrame:startingPointFrame];
[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
NSArray *keys = [letterDictionary allKeys];
int nKey = [keys count];
for(int k1 = 0; k1 < nKey; ++ k1) { for(int k2 = k1 + 1; k2 < nKey; ++ k2) {
if ( CGRectIntersectsRect( ((UIImageView*) [letterDictionary objectForKey:[keys objectAtIndex:k1]]).frame, ((UIImageView*) [letterDictionary objectForKey:[keys objectAtIndex:k2]]).frame ) ) {
NSLog(@"k1: %i k2: %i",k1,k2);
}
}
}
}
}
Thanks!
You are adding 5 UIImageViews
per letter to your NSDictionary
. All views of a letter are the same dimension at the same location. ie. all A
views are the same size at the same place. Therefore, when you loop the dictionary checking for intersections, it makes sense that you would see a lot more intersections than you expect.
Perhaps what would make more sense for you is to change your problem statement from see if one of the letters intersects with another from the same dictionary to see if the moved letter intersects with another active letter.
You could track active letters by adding them to an NSArray
the first time they are moved. Maybe you would also want to designate the top view for each letter an active view as well. Then, when you check for intersections, you only check your active letters.
精彩评论