简体   繁体   English

UIView Center是否在视图中更新,这取决于是否在代码中添加另一个uiimageview

[英]UIView center updates in view or not depending on whether I add another uiimageview in code

I have created 2 arrays of UIViews (game pieces, clue). 我创建了2个UIViews数组(游戏片段,线索)。 I am trying to update the center of some of these UIViews. 我正在尝试更新其中一些UIView的中心。 The center updates in the simulator if I don't add a view after updating their centers. 如果更新中心后未添加视图,则模拟器中的中心将更新。 However, it doesn't update if I do add another view. 但是,如果我添加其他视图,它不会更新。 Can you help me understand what is going on here, and how I get the UIviews in the array to update all the time ? 您能帮助我了解这里发生的情况以及如何使数组中的UIview始终保持更新吗?

Sample code below : (note - the for loops loop through the UIView array to determine which one will be updated) 下面的示例代码:(注意-for循环遍历UIView数组以确定将更新哪一个)

    for (int i=0; i<[newPositionArray count]; i++)
    {
        NSString *imageIdentifier = [newPositionArray objectAtIndex:i];
        CGPoint newCenter;
        newCenter = .... code to calculate new center

        if ([imageIdentifier compare:@""])
        {
            BOOL clueFound = NO;
            for (UIView *piece in _gamePieces)
            {
                if (![[piece  accessibilityIdentifier] compare:imageIdentifier])
                {
                    piece.center = newCenter;
                    [piece setAlpha:0.5];
                    clueFound = YES;
                }
            }

            if (!clueFound)
            {
                for (UIView *clue in _attributeClues)
                {
                    if (![[clue accessibilityIdentifier] compare:imageIdentifier])
                    {
                        CGRect oldViewRect = CGRectMake(clue.center.x, clue.center.y-22, 44, 44);

                        [clue setAlpha:0.5];
                        clueFound = YES;
                        [clue setCenter:newCenter];

                        UIImageView *newimage = [[UIImageView alloc]initWithFrame:oldViewRect];
                        [newimage setImage:[[clue.subviews objectAtIndex:0] image]];
                        [newimage setAccessibilityIdentifier:imageIdentifier];
                        [self.view addSubview:newimage];

                    }
                }
            }
        }
    }
}

} }

if I comment out the last line of code above, then the other views change centers in the simulator. 如果我注释掉上面代码的最后一行,则其他视图将更改模拟器的中心。 If I don't comment the last line of code, then they don't change centers. 如果我不注释最后一行代码,那么它们就不会更改中心。 In either case, the alpha of the views does change. 在任何一种情况下,视图的Alpha确实会更改。 I'd like to add the view, and update the centers. 我想添加视图并更新中心。

I found an answer that works. 我找到了一个有效的答案。 The function above was being called within another function, and the centers were being updated as local variables that were lost on exiting the above function. 上面的函数在另一个函数中被调用,并且中心被更新为局部变量,这些局部变量在退出上述函数时丢失。 So I solved it by storing the centers in a dictionary, and updating the centers in the calling function of this function. 因此,我通过将中心存储在字典中并在此函数的调用函数中更新中心来解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM