简体   繁体   中英

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). I am trying to update the center of some of these UIViews. 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 ?

Sample code below : (note - the for loops loop through the UIView array to determine which one will be updated)

    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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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