2011-09-27 63 views
0

我在一個NSMutableDictionary中有一堆UIImageViews。獲取視圖正在平移的字典鍵並將其添加到數組

我的ImageView的創建方法:

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]; //Adds the letter a UIImageView to dictionary with key: a1, a2, a3, a4, a5. 
    [self.view addSubview:alphabetVowelA]; 
    [alphabetVowelA release]; 
} 

這裏是我的移動手勢的方法。

- (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) 
    { 

     pannedPieces = [[NSMutableArray alloc] init]; 

     [pannedPieces addObject:piece]; 


     NSEnumerator *enumerator = [pannedPieces objectEnumerator]; 
     id element; 

     NSUInteger arrayCount = [pannedPieces count]; 

     while(element = [enumerator nextObject]) 
     { 
      NSUInteger i = [pannedPieces indexOfObject:element]; 
      NSLog(@"%@ \n @index %i \n Array Count: %i",element, i, arrayCount); 
     } 
} 

我想補充的是被搖鏡和下降到一個NSMutableArray,這樣我可以與那些獨立意見我的所有其他視圖的工作意見。當我用上面的代碼做時,看起來每個被平移的塊都不會將自己添加到數組的末尾,它只會將它放在索引0處。所以我的@index輸出0,而Array Count打印1,無論我已經平移了多少個視圖。

而不是隻是添加被淘汰的「片」,我怎樣才能得到被平移的UIImageView的字典鍵,以便我可以通過鍵將它添加到數組?

我希望這個問題有道理。

回答

2

這條線:

pannedPieces = [[NSMutableArray alloc] init]; 

每次進入該分支(可能還泄漏)時間覆蓋pannedPieces一個新的陣列。如果你想收集更多的部分,不要使用新的數組,只需添加到現有的數組。

要從字典中獲取對象的密鑰,請使用-allKeysForObject:

+0

兒子的一個..杜。呵呵。謝謝!我仍然非常好奇,如何從我的字典中獲得被淘汰或被淘汰的圖像的關鍵字。你能否讓我知道我該如何做到這一點? – Jason

相關問題