我是Objective C新手,已經查找了幾天但找不到答案。我在容器視圖中有9個圖像視圖,並且每個視圖都只能被點擊一次,我想在隨機5個UIImageView被點擊後執行一些操作。以下是我用於每個人體驗一次點擊的代碼。如果必須更改我的代碼,請提供任何幫助。計算已從多個UIViews中隨機點擊的UIViews的數量,即:從9個UIViews中點擊隨機5個UIViews,然後執行操作
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moveToNextMovingRectangle:)];
tapGestureRecognizer.numberOfTouchesRequired = 1;
tapGestureRecognizer.numberOfTapsRequired = 1;
[self.imageView1 addGestureRecognizer:tapGestureRecognizer];
[self.imageView2 addGestureRecognizer:tapGestureRecognizer];
[self.imageView3 addGestureRecognizer:tapGestureRecognizer];
[self.imageView4 addGestureRecognizer:tapGestureRecognizer];
[self.imageView5 addGestureRecognizer:tapGestureRecognizer];
[self.imageView6 addGestureRecognizer:tapGestureRecognizer];
[self.imageView7 addGestureRecognizer:tapGestureRecognizer];
[self.imageView8 addGestureRecognizer:tapGestureRecognizer];
[self.imageView9 addGestureRecognizer:tapGestureRecognizer];
// if (5 random imageShapes have been tapped){
//
// do something
// }
僅供參考,A UITapGestureRecognizer實例只能依附於到一個觀點。您需要爲每個視圖製作一個新視圖。 – 2014-10-18 12:31:23
tapGestureRecognizer.numberOfTapsRequired = 9;應該管用。 – 2014-10-18 12:42:05