[英]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.