我正在開展掃雷遊戲,我想在用戶長時間點擊遊戲板的圖塊時添加標誌。 我實現了這個代碼:UIButton上的長按手勢識別器?
對於每一個按鈕,在遊戲鍵盤:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressTap:)];
longPress.minimumPressDuration = 1.0f;
[self.button addGestureRecognizer:longPress];
在自我的方法longPressTap:
- (void)longPressTap:(Tile *)sender {
if (sender.block.marking == MARKING_FLAGGED) {
// if already a flag I mark as a blank tile, with color defined for gameboard
sender.backgroundColor = UIColorFromRGB(0x067AB5);
sender.block.marking = MARKING_BLANK;
self.flagCount++;
}
else{
// if it's not a flag I mark as a flag and set the flag image for the tile
[sender setBackgroundImage:[UIImage imageNamed:IMAGE_NAME_FLAG] forState:UIControlStateNormal];
sender.block.marking = MARKING_FLAGGED;
self.flagCount--;
}
}
當然自我的,是我的UIGestureRecognizerDelegate 。 但是當我嘗試長按上一瓦,應用程序崩潰,給這個錯誤:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILongPressGestureRecognizer block]: unrecognized selector sent to instance 0x8cf2b00'
我該怎麼辦?我剛剛開始使用Obj-C編程,所以如果有人可以幫我解釋我做錯了什麼,我會非常感激。
是啊,這就是我的想法是錯的,但經過UILongPressGestureRecognizer作爲參數longPressTap: 我怎麼能檢查哪些瓷磚用戶選擇並將其標記爲標誌/空白?對不起noob問題,但我想了解 –
使用'[發送者位置查看]'從那裏找到觸摸的位置,你應該有一個邏輯來獲得相關的'瓦片' –
嘗試'[發送者視圖]'也可能工作 –