2013-02-04 69 views
2

我想檢測UILongPressGestureRecognizerUIWebView分接開關不放..such,當我長按近3秒鐘,然後下面的if條件應該是True那麼只有
if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture)但它不工作... 。它繼續在迴路每次..does爲longPressGesture不及時檢查...UILongPressGestureRecognizer不工作

即使我已經與條件試過..

if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture.minimumPressDuration> 3)

不working..where我會犯錯..

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 


UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] init]; 

longGesture.numberOfTapsRequired = 1; 
longGesture.numberOfTouchesRequired = 1; 
longGesture.minimumPressDuration = 3 
; 
longGesture.delegate = self; 
// longGesture.allowableMovement = 50; 

[self.webView addGestureRecognizer:longGesture]; 



if (navigationType == UIWebViewNavigationTypeLinkClicked && longGesture) 
{ 
    // Call your custom actionsheet and use the requestURL to do what you want :) 


    UIActionSheet *sheet = [[UIActionSheet alloc] 
          initWithTitle:@" OPTIONS " 
          delegate:self 
          cancelButtonTitle:nil 
          destructiveButtonTitle:nil 
          otherButtonTitles:nil]; 


    [sheet addButtonWithTitle:@"Open"]; 
    [sheet addButtonWithTitle:@"Copy"]; 

    // Set cancel button index to the one we just added so that we know which one it is in delegate call 
    // NB - This also causes this button to be shown with a black background 
    sheet.cancelButtonIndex = sheet.numberOfButtons-1; 

    [sheet showInView:webView]; 
    return NO; 
    } 

回答

3

你應該能夠同時手勢識別,因爲UIWebView中設置了幾個識別器本身,你會被跳過: 在代碼中添加此

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    return YES; 
} 
+0

沒有不工作的男人! – Christien

+0

意味着如果我使用'longGesture.minimumPressDuration = 3' ;和'if(navigationType == UIWebViewNavigationTypeLinkClicked && longGesture.minimumPressDuration)'它使if條件爲TRUE而不保持3秒 – Christien

+0

@MilKyWaY - 我在幾個月前遇到了一個問題,我努力讓LongTouch使用普通的觸摸識別器 - 並且最終放棄了而不是使用longtouch,因爲我認爲兩者都是不可能的。然後我看到這個。非常感謝你。 –

1

你沒有爲你的手勢識別器設置目標動作,對嗎?

UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureUpdated:)]; 

設置一個目標動作會讓你得到通知,如果手勢觸發或沒有! 我首先用這種方法開始檢查是否調用了「longPressGestureUpdated」方法。

請嘗試以下的定義可能:

longPGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureUpdated:)]; 
    longPressGesture.numberOfTouchesRequired = 1; 
    longPressGesture.delegate = self; 
    longPressGesture.cancelsTouchesInView = NO; 

(而且可以同時進行手勢識別爲銀河建議的話)

+0

嘿聽...目標行動被調用根據我設置的minimumPressDuration(3秒)...現在問題是在'IF條件'如果(導航類型== UIWebViewNavigationTypeLinkClicked && longGesture)'它應該是TRUE 3秒後..但它不檢查longGesture時間 – Christien

+0

NENAD是正確的,如果你沒有設置一個動作到longPressGesture對象,這個動作永遠不會發生。你應該在這裏閱讀完整的蘋果文檔(關於手勢):http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html – Ashbay

+0

是否可以掛鉤你的if-stmt。 「longPressGestureUpdated」操作方法中的邏輯? –

2
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)]; 
    longPress.numberOfTouchesRequired = 1; 
    [self.view addGestureRecognizer:longPress]; 
} 

-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture 
{ 
    if (gesture.state == UIGestureRecognizerStateBegan) 
    { 
    UIActionSheet *sheet = [[UIActionSheet alloc] 
         initWithTitle:@" OPTIONS " 
         delegate:self 
         cancelButtonTitle:nil 
         destructiveButtonTitle:nil 
         otherButtonTitles:nil]; 


    [sheet addButtonWithTitle:@"Open"]; 
    [sheet addButtonWithTitle:@"Copy"]; 

    // Set cancel button index to the one we just added so that we know which one it is in delegate call 
    // NB - This also causes this button to be shown with a black background 
    sheet.cancelButtonIndex = sheet.numberOfButtons-1; 

    [sheet showInView:webView]; 
    } 
} 
+0

你好,謝謝你!但我希望'UILongPressGestureRecognizer'應該只在某些條件爲'link(鏈接)'的條件下完成if(navigationType == UIWebViewNavigationTypeLinkClicked)'..我想'UILongPressGestureRecognizer' – Christien

+0

這樣if(導航類型== UIWebViewNavigationTypeLinkClicked && longGesture.minimumPressDuration> 3)'意味着如果它是鏈接,並在該鏈接上點擊3秒...然後uiactionsheet將顯示 – Christien

相關問題