[英]How do I programmatically add a UIImage to a UIImageView then add that UIImageView to another UIView?
[英]UIView center updates in view or not depending on whether I add another uiimageview in code
我创建了2个UIViews数组(游戏片段,线索)。 我正在尝试更新其中一些UIView的中心。 如果更新中心后未添加视图,则模拟器中的中心将更新。 但是,如果我添加其他视图,它不会更新。 您能帮助我了解这里发生的情况以及如何使数组中的UIview始终保持更新吗?
下面的示例代码:(注意-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];
}
}
}
}
}
}
}
如果我注释掉上面代码的最后一行,则其他视图将更改模拟器的中心。 如果我不注释最后一行代码,那么它们就不会更改中心。 在任何一种情况下,视图的Alpha确实会更改。 我想添加视图并更新中心。
我找到了一个有效的答案。 上面的函数在另一个函数中被调用,并且中心被更新为局部变量,这些局部变量在退出上述函数时丢失。 因此,我通过将中心存储在字典中并在此函数的调用函数中更新中心来解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.