iOS中的Swift-Gomoku
我学Swift已经两个月了。我正在尝试用iOS重写Gomoku游戏。我的游戏允许玩家玩电脑。我遇到了一个与视图相关的问题。我有一个自定义的UICollectionView 在我的UICollectionView中有指定数量的单元格。当玩家与UICollectionViewCell交互时,符号“x”或“o”会立即显示,玩家必须在下一个回合前等待计算机。下面是我的代码iOS中的Swift-Gomoku,ios,iphone,swift,swift2,Ios,Iphone,Swift,Swift2,我学Swift已经两个月了。我正在尝试用iOS重写Gomoku游戏。我的游戏允许玩家玩电脑。我遇到了一个与视图相关的问题。我有一个自定义的UICollectionView 在我的UICollectionView中有指定数量的单元格。当玩家与UICollectionViewCell交互时,符号“x”或“o”会立即显示,玩家必须在下一个回合前等待计算机。下面是我的代码 func collectionView(collectionView: UICollectionView, didSelectIte
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let square = collectionView.cellForItemAtIndexPath(indexPath) as! Square
if square.isEmpty(){
// determine which player is available
let curPlayer = game.isTurnPlayer1 ? game.player1 : game.player2
if curPlayer is Human {
//set symbol into each cell
square.setSymbol(curPlayer.piece)
// set move in board (model handling)
{...}
// computer will decide to move and change turn
self.didMakeMove(indexPath, piece: curPlayer.piece)
}
}
}
每当玩家选择UICollectionViewCell(他移动)时,调用上面的方法,我知道当且仅当该方法被释放时视图才会更新,但我希望视图显示,而不必等待完成方法“didMakeMove”。
我尝试在didMakeMove中使用dispatch_async,如下所示:
func didMakeMove(indexPath: NSIndexPath, piece: String) {
//check if game is over
{...}
////////// dispatch_async
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)){
//calculate next move for computer
{...}
}
}
然后我运行游戏,我意识到电脑的速度太慢,无法计算。然而,当我尝试用
调度获取主队列()
替换调度队列优先级高
时,计算机计算移动速度更快。但有时,我必须等到完成计算后,才能在移动后看到“x”和“o”。我知道当且仅当该方法发布时,视图才会更新。。我至少不清楚这一点。我的意思是,如果带有参数didSelectedIndexPath的方法collectionview完成,视图将被更新,否则不会更新。我不确定您的问题是什么。。我的问题是,在玩家与手机交互后,如何让符号“x”,“o”(取决于谁先走)立即显示出来,这样,就不必关心计算机计算的时间了。好吧,你有了聊天代表。。并解释你的问题。那太好了