2014-10-18 66 views
1

我是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 
// } 
+0

僅供參考,A UITapGestureRecognizer實例只能依附於到一個觀點。您需要爲每個視圖製作一個新視圖。 – 2014-10-18 12:31:23

+0

tapGestureRecognizer.numberOfTapsRequired = 9;應該管用。 – 2014-10-18 12:42:05

回答

0

創建一個新的計數器變量和增加,而在圖像用戶水龍頭每一次它的價值,當它達到5,你可以做任何你want.Like這樣:

首先聲明globel計數器變量在ViewController類如:

var counter : Int = Int() 


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]; 
self.moveToNextMovingRectangle() 

} 


    func moveToNextMovingRectangle(){ 

// Increment in counter value 
    counter += 1; 
    if (counter>=5) 
    { 
     //do what ever you want 
    } 
    else { 
     //get a random number using arc4random() method and show your next imageview 
    } 
} 

我認爲這將工作嘗試它。

+0

我該如何製作計數水龍頭數量的計數器?我製作的計數器無法計算水龍頭或拍攝的圖像。 – 2014-10-18 13:43:16

0

想,如果我們有救了我們在NSArray中和反imagesviews跟蹤水龍頭

NSArray *imgViews = @[self.imageView1,self.imageView2.....,self.imageView9]; 
int counter; 

後,在你選擇做這個

-(void) moveToNextMovingRectangle{ 
counter +- 1; 
    if (counter>=5) 
    { 
     //do what ever you want 
    } 
    else { 
     //get a random number using arc4random() method and show your next imageview 
    } 
} 
+0

我似乎無法將點擊數量與計數器或點擊的imageViews鏈接到計數器。對於像我這樣的新人,請說清楚。 – 2014-10-18 13:40:47

+0

我們使用了一個名爲counter的全局變量,並在imageview的每次敲擊時增加它,請參閱moveToNextMovingRectangle selector – ZAZ 2014-10-18 14:07:24