gpt4 book ai didi

iphone - 获取正在平移的 View 的字典键并将其添加到数组中

转载 作者:行者123 更新时间:2023-11-28 23:12:58 26 4
gpt4 key购买 nike

我在 NSMutableDictionary 中有一堆 UIImageView。

我的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);
}
}

我正在尝试将平移和删除的 View 添加到 NSMutableArray 中,以便我可以独立于所有其他 View 处理这些 View 。使用我上面的代码时,似乎每一个被平移的片段都没有将自己添加到数组的末尾,它每次只将它放在索引 0 处。因此,无论我平移了多少 View ,我的@index 都会打印 0,Array Count 会打印 1。

不是只添加被平移的“片段”,而是如何获取被平移的 UIImageView 的字典键,以便我可以将它通过键添加到数组中?

我希望这个问题是有道理的。

最佳答案

这一行:

pannedPieces = [[NSMutableArray alloc] init];

每次进入该分支时都会用新数组覆盖 pannedPieces(也可能会泄漏)。如果您想收集更多碎片,请不要使用新数组,只需添加到现有数组即可。

要从字典中获取对象的键,请使用 -allKeysForObject: .

关于iphone - 获取正在平移的 View 的字典键并将其添加到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7576872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com