我有5個UIImageViews存儲在一個NSMutableDictionary中的每個字母的字母。 5而不是1,以防萬一他們想拼出一個單詞來呼叫更多相同的字母。如何檢查NSMutableDictionary中的UIImageView是否與相同的NSMutableDictionary中的另一個UIImageView相交?
我創建這樣的信件...... 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消息。以下是它的輸出示例。
這個輸出發生,即使沒有字典中的UIImageViews的交叉。它實際上是在平移之後發生的,然後放下第一個字母平鋪,然後再拾取第二個平鋪,以便與下降相交。我無法弄清楚爲什麼。我的平底鍋:代碼如下。
- (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);
}
}
}
}
}
謝謝!
謝謝!這很有道理。閱讀你的答案時肯定是一個「唔」的時刻。 :) – Jason