2013-03-09 55 views
0

我創建了一個應用程序。通過開發它,我用長按鈕顯示警報。UIAlertView警報在長按手勢識別器內重複三次

這是我的代碼:

- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender { 
    //  label1.text = @"Select Iran to observe its historical data projections "; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                message:@"Press the blue button (+) to select your region " 
                delegate:self 
              cancelButtonTitle:@"Ok" 
              otherButtonTitles:nil]; 

    [alert show];  
} 

問:當我想通過cancelIbutton取消UIAlertView中,我必須按下此按鈕三次,取消了UIAlertView中。這意味着UIAlterview只能通過一次按下才能取消。 你能幫我嗎?

+1

這與PHP有什麼關係? – Jodes 2013-03-09 03:56:20

+0

對不起,這是ipad應用程序 – hooman 2013-03-09 04:13:06

回答

0

試試這個

- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender { 
//  label1.text = @"Select Iran to observe its historical data projections "; 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
               message:@"Press the blue button (+) to select your region " 
               delegate:self 
             cancelButtonTitle:@"Ok" 
             otherButtonTitles:nil]; 
alert.tag =10; 
[alert show];  

} 

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { 

    switch (alertView.tag) 
{ 
    case 10: 
     if (buttonIndex==0) 
     { 

     } 
     break; 
    } 
} 
+0

這是如何解決顯示多個警報視圖或需要點擊取消三次的問題? – rmaddy 2013-03-09 04:37:06

+0

我想你在視圖控制器中使用多於一個的alertview – Ravindhiran 2013-03-09 04:42:03

9

的問題是,您都出現了不止一個警報視圖。你的長按處理程序將被調用不同的狀態。

從文檔的UILongPressGestureRecognizer

長按手勢是連續的。當在指定的時間段(minimumPressDuration)按下允許的手指的數量(numberOfTouchesRequired)並且觸摸不超過允許的移動範圍(allowableMovement)時,手勢開始(UIGestureRecognizerStateBegan)。手指移動時,手勢識別器轉換到「更改」狀態,並且當任何手指擡起時手勢識別器結束(UIGestureRecognizerStateEnded)。

因此,您最終顯示「開始」狀態,然後是「已更改」狀態,並再次顯示「已結束」狀態的警報。如果你移動你的手指,你會得到一個「改變」狀態的流,你最終也會爲他們每個人顯示一個警報。

您需要確定何時實際希望顯示警報。你想讓它出現在第一個「改變」的狀態,或者當用戶舉起他們的手指,你得到「結束」的狀態。

你的代碼需要是這樣的:

- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) { 
     //  label1.text = @"Select Iran to observe its historical data projections "; 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                message:@"Press the blue button (+) to select your region " 
                delegate:self 
              cancelButtonTitle:@"Ok" 
              otherButtonTitles:nil]; 

     [alert show]; 
    } 
} 

當用戶提起他們的手指這將顯示警報。如果您希望在識別長按時立即顯示警報,請將UIGestureRecognizerStateEnded更改爲UIGestureRecognizerStateChanged。但請記住,由於長按可以生成多個「更改」狀態,因此您仍然可以獲得倍數。在這種情況下,您需要添加一個額外的檢查,以便只顯示警報,如果還沒有。

其實,這裏有一個簡單的方法來支持的「更改」狀態的單個警報:

- (IBAction)longPressDetected1:(UIGestureRecognizer *)sender { 
    if (sender.state == UIGestureRecognizerStateChanged) { 
     sender.enabled = NO; // Prevent any more state updates so you only get this one 
     sender.enabled = YES; // reenable the gesture recognizer for the next long press 

     //  label1.text = @"Select Iran to observe its historical data projections "; 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                message:@"Press the blue button (+) to select your region " 
                delegate:self 
              cancelButtonTitle:@"Ok" 
              otherButtonTitles:nil]; 

     [alert show]; 
    } 
} 
+0

好的答案,當你希望在長按被識別時出現警報,你需要與UIGestureRecognizerStateBegan進行比較,而不是......改變。 – fishinear 2013-11-27 18:03:27

0

這是一個老問題,但我也有類似的問題。

任何UILongPressGestureRecognizer會產生狀態的至少4點的變化:

  • 改變 - (即變更爲開始)
  • 開始 - (即啓動)
  • 改變 - (即變更爲結束)
  • 已結束 - (即結束)

所以要處理好,你需要選擇哪些條件將激活一個動作。

對於UIButton來說,最簡單的對應於'touch up inside'的方法是檢測'已結束'狀態。

下面是一個例子,其中「長按」是UILongPressGestureRecognizer

- (void)longPressedLastImage:(UILongPressGestureRecognizer*) longPress { 
    if (longPress.state == UIGestureRecognizerStateEnded) { 
     // do what you would in response to an equivalent button press 
    } 
} 

的情況下這可以讓你把連續按動更像是基本的UIButton的行動。

'簡單比複雜'更好 - T彼得斯 - Python的禪宗