如何检查NSMutableDictionary中的UIImageView是否与同一NSMutableDictionary中的另一个UIImageView相交?

时间:2011-09-09 23:56:20

标签: ios ipad drag-and-drop uiimageview nsmutabledictionary

对于存储在NSMutableDictionary中的每个字母的字母,我有5个UIImageViews。 5而不只是1,以防万一他们想要拼写的单词要求更多相同的字母。

我正在创建这样的字母...... 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];
    }

使用下面的代码,我正在尝试检查其中一个字母是否与同一个字典中的另一个字母相交。但是,使用我的代码,当我平移/拖动其中一个字母图块然后删除它时,它会为字典中的每个键输出一条NSLog消息。以下是其输出的示例。

http://pastie.org/2511092

即使字典中的UIImageViews都没有交叉,也会发生此输出。它实际上发生在平移之后然后丢弃第一个字母图块,甚至在拾取第二个字母以使其与下降相交之前。我无法弄清楚为什么。我的pan:代码在下面。

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);

            }
        }

        }
    }

}

谢谢!

1 个答案:

答案 0 :(得分:1)

您在UIImageViews的每封信中添加了5 NSDictionary。字母的所有视图在同一位置都是相同的维度。即。所有A个视图在同一位置的大小相同。因此,当您循环检查交叉点的字典时,您会看到比预期更多的交叉点。

对你来说更有意义的是从改变你的问题陈述,看看其中一个字母是否与同一个字典中的另一个字母相交看看移动的字母是否与另一个字母相交活跃的信。

您可以在第一次移动活动时将其添加到NSArray来跟踪活动信件。也许您还希望为活动视图的每个字母指定顶视图。然后,当您检查交叉点时,只检查活动字母。