2010-10-11 111 views
1

如何檢測UIScrollview中的雙擊?檢測雙擊UIScrollView

我想:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 

    if (touch.tapCount == 2) { 
     // some operations here 
    } 
} 

但這不是檢測UIScrollView中雙擊,有沒有其他的方法來檢測雙擊?

問候

+0

只是在發佈時格式化一點。如果您將代碼複製並粘貼到您的問題中並突出顯示所有內容,然後單擊輸入框上方的011010按鈕,則會正確格式化它。 – 2010-10-11 10:12:46

回答

6

使用下面的代碼進行雙擊。

-(void) viewDidLoad{ 
    // other necessary operations 
    UITapGestureRecognizer* dTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (doubleTap:)]; 

    [doubleTap setDelaysTouchesBegan : YES]; 
    dTap.numberOfTapsRequired = 2; 
    dTap.numberOfTouchesRequired = 1; 
    [self.view addGestureRecognizer : dTap]; 
    [dTap release]; 
} 

- (void) doubleTap : (UIGestureRecognizer*) sender 
{ 
    NSLog (@"Double tap Do operations here..."); 
} 
+0

一切都好,但選擇器名稱應該doubleTap我猜... – 2012-11-06 19:37:32

+0

是的,你是對的。這是錯誤的。謝謝你的糾正。 – krunal 2012-12-15 06:38:16

2
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSUInteger numTaps = [[touches anyObject] tapCount]; 
    if (numTaps== 2) { 
     //some operations here 
    } 
} 

使用此代碼

+0

感謝Gyani回覆,但scrollview沒有檢測到任何觸摸事件....有沒有其他方式來檢測滾動視圖中的雙標籤? – iscavengers 2010-10-11 10:25:05

+0

你可以從http://iphonedevelopertips.com/user-interface/detect-single-tap-in-uiscrollview.html – Gyani 2010-10-11 10:35:34

17

如果你的目標3.2+,你可以嘗試使用UITapGestureRecognizer

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:yourViewController action:@selector(tapGesture:)]; 
tap.numberOfTapsRequired = 2; 
tap.numberOfTouchesRequired = 1; 
[scrollView addGestureRecognizer:tap]; 
[tap release]; 

然後你處理您的ViewController水龍頭:

- (void)tapGesture:(UIGestureRecognizer*)gesture { 
    // Do something 
} 
+0

得到一些幫助謝謝aegzorz,將嘗試你的代碼段... – iscavengers 2010-10-12 10:51:18