UIImageView无法更新

时间:2012-08-25 13:54:33

标签: ios uiviewcontroller uiimageview uiimage

我目前正在为iPhone制作国际象棋游戏。电路板上的部件由一系列UIImageViews表示,每个UIImageViews代表电路板的正方形,并引用正确的图像以显示该正方形。已经选择了一个正方形并且已经移动了,函数 - (void)updateBoard;在ViewController中调用:

-(void)updateBoard{

NSLog(@"updating board");

[square1 setImage:[UIImage imageNamed:[game imageForSquare:1]]];
[square2 setImage:[UIImage imageNamed:[game imageForSquare:2]]];
[square3 setImage:[UIImage imageNamed:[game imageForSquare:3]]];
[square4 setImage:[UIImage imageNamed:[game imageForSquare:4]]];
[square5 setImage:[UIImage imageNamed:[game imageForSquare:5]]];
[square6 setImage:[UIImage imageNamed:[game imageForSquare:6]]];
[square7 setImage:[UIImage imageNamed:[game imageForSquare:7]]];
[square8 setImage:[UIImage imageNamed:[game imageForSquare:8]]];
[square9 setImage:[UIImage imageNamed:[game imageForSquare:9]]];
[square10 setImage:[UIImage imageNamed:[game imageForSquare:10]]];
[square11 setImage:[UIImage imageNamed:[game imageForSquare:11]]];
[square12 setImage:[UIImage imageNamed:[game imageForSquare:12]]];
[square13 setImage:[UIImage imageNamed:[game imageForSquare:13]]];
[square14 setImage:[UIImage imageNamed:[game imageForSquare:14]]];
[square15 setImage:[UIImage imageNamed:[game imageForSquare:15]]];
[square16 setImage:[UIImage imageNamed:[game imageForSquare:16]]];
[square17 setImage:[UIImage imageNamed:[game imageForSquare:17]]];
[square18 setImage:[UIImage imageNamed:[game imageForSquare:18]]];
[square19 setImage:[UIImage imageNamed:[game imageForSquare:19]]];
[square20 setImage:[UIImage imageNamed:[game imageForSquare:20]]];
[square21 setImage:[UIImage imageNamed:[game imageForSquare:21]]];
[square22 setImage:[UIImage imageNamed:[game imageForSquare:22]]];
[square23 setImage:[UIImage imageNamed:[game imageForSquare:23]]];
[square24 setImage:[UIImage imageNamed:[game imageForSquare:24]]];
[square25 setImage:[UIImage imageNamed:[game imageForSquare:25]]];
[square26 setImage:[UIImage imageNamed:[game imageForSquare:26]]];
[square27 setImage:[UIImage imageNamed:[game imageForSquare:27]]];
[square28 setImage:[UIImage imageNamed:[game imageForSquare:28]]];
[square29 setImage:[UIImage imageNamed:[game imageForSquare:29]]];
[square30 setImage:[UIImage imageNamed:[game imageForSquare:30]]];
[square31 setImage:[UIImage imageNamed:[game imageForSquare:31]]];
[square32 setImage:[UIImage imageNamed:[game imageForSquare:32]]];
[square33 setImage:[UIImage imageNamed:[game imageForSquare:33]]];
[square34 setImage:[UIImage imageNamed:[game imageForSquare:34]]];
[square35 setImage:[UIImage imageNamed:[game imageForSquare:35]]];
[square36 setImage:[UIImage imageNamed:[game imageForSquare:36]]];
[square37 setImage:[UIImage imageNamed:[game imageForSquare:37]]];
[square38 setImage:[UIImage imageNamed:[game imageForSquare:38]]];
[square39 setImage:[UIImage imageNamed:[game imageForSquare:39]]];
[square40 setImage:[UIImage imageNamed:[game imageForSquare:40]]];
[square41 setImage:[UIImage imageNamed:[game imageForSquare:41]]];
[square42 setImage:[UIImage imageNamed:[game imageForSquare:42]]];
[square43 setImage:[UIImage imageNamed:[game imageForSquare:43]]];
[square44 setImage:[UIImage imageNamed:[game imageForSquare:44]]];
[square45 setImage:[UIImage imageNamed:[game imageForSquare:45]]];
[square46 setImage:[UIImage imageNamed:[game imageForSquare:46]]];
[square47 setImage:[UIImage imageNamed:[game imageForSquare:47]]];
[square48 setImage:[UIImage imageNamed:[game imageForSquare:48]]];
[square49 setImage:[UIImage imageNamed:[game imageForSquare:49]]];
[square50 setImage:[UIImage imageNamed:[game imageForSquare:50]]];
[square51 setImage:[UIImage imageNamed:[game imageForSquare:51]]];
[square52 setImage:[UIImage imageNamed:[game imageForSquare:52]]];
[square53 setImage:[UIImage imageNamed:[game imageForSquare:53]]];
[square54 setImage:[UIImage imageNamed:[game imageForSquare:54]]];
[square55 setImage:[UIImage imageNamed:[game imageForSquare:55]]];
[square56 setImage:[UIImage imageNamed:[game imageForSquare:56]]];
[square57 setImage:[UIImage imageNamed:[game imageForSquare:57]]];
[square58 setImage:[UIImage imageNamed:[game imageForSquare:58]]];
[square59 setImage:[UIImage imageNamed:[game imageForSquare:59]]];
[square60 setImage:[UIImage imageNamed:[game imageForSquare:60]]];
[square61 setImage:[UIImage imageNamed:[game imageForSquare:61]]];
[square62 setImage:[UIImage imageNamed:[game imageForSquare:62]]];
[square63 setImage:[UIImage imageNamed:[game imageForSquare:63]]];
[square64 setImage:[UIImage imageNamed:[game imageForSquare:64]]];


NSLog(@"-------------------BOARD READOUT-----------------------:");

for (int i = 1; i <=64; i++) {
    NSLog(@"Square %i: %@", i, [game imageForSquare:i]);
}

}

'square1','square2'等是UIImageViews,[game imageForSquare:]方法返回需要显示的相应图像的NSString *。

当玩两个人类玩家时,棋盘会完美更新,之后程序会等待下一个玩家的输入,而且我已经运行了很多游戏而没有任何问题。

我还有一个使用算法选择其移动的计算机播放器,我目前正在尝试与两个计算机玩家建立一个游戏。 (我还没有尝试过人类与电脑游戏)。

问题在于,当调用updateBoard方法时,没有任何图像被更新,尽管底部的NSLog读数告诉我一切正常,我实际上可以看到两个玩家通过控制台玩游戏。 / p>

我认为这与调用updateBoard之后立即将控制权传递给下一个计算机播放器并以某种方式阻止UIImageViews正确更新的事实有关。我做了一个测试来评论调用下一个玩家的代码行(意味着只进行了一次移动,然后执行停止)并且电路板确实正确更新。

很抱歉这个很长的问题,请提前感谢任何建议!

1 个答案:

答案 0 :(得分:15)

仅在程序控制返回主runloop时更新用户界面。你应该在后台线程中进行计算,然后调用

dispatch_async(dispatch_get_main_queue(), ^{
   [self updateBoard];
});
当您想要更新显示时,从后台线程

。然后,在进行计算时,您不会阻止UI。