我在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,无论我淘汰了多少个视图,数组计数都会打印1。
不是只添加被淘汰的“片段”,而是如何获取平移UIImageView的字典键,以便我可以通过键将其添加到数组中?
我希望这个问题有道理。
答案 0 :(得分:2)
这一行:
pannedPieces = [[NSMutableArray alloc] init];
每次进入该分支时,都会用新数组覆盖pannedPieces
(可能还会泄漏)。如果要收集更多碎片,请不要使用新阵列,只需添加到现有阵列即可。
要从字典中获取对象的密钥,请使用-allKeysForObject:
。