2013-05-03 75 views
1

我已經分類UIScrollView和執行 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 方法在其中。沒有得到從UIScrollView觸摸

但點擊或按住滾動視圖時沒有任何反應!

編輯:我還需要使用touchesEnded方法

+0

您能否提供更重要的信息?你是通過Storyboard創建你的UIScrollView嗎?確保觸摸事件已啓用。 – David 2013-05-03 06:41:55

+0

不,我不使用故事板或.xib文件。 – 2013-05-03 06:42:55

+0

http://stackoverflow.com/questions/5216413/uiscrollview-getting-touch-events – Balu 2013-05-03 06:43:26

回答

2

我想UIScrollView中有兩個手勢識別。他們負責處理觸摸序列,因此他們吞下觸摸事件。

使用滾動視圖的委託方法來處理拖拽手勢或

touchesShouldBegin:withEvent:方法inContentView:

方法來處理滾動視圖內容風格和

touchesShouldCancelInContentView:

取消它。

作爲替代方案,您可以操作scrollView的panGesture識別器以將事件傳遞給下一個響應者。

+0

這就是我在找的東西! UIScrollView有什麼方法在觸摸結束時被調用? – 2013-05-03 07:11:10

+0

是的,但正如我告訴事件調用被手勢攔截。使用touchesShouldBegin:withEvent:inContentView:爲您的特殊視圖返回YES,以便它們將獲取已觸及的開始/結束事件 – Peteee24 2013-05-04 14:08:47

1

在你的UIScrollView的子類中覆蓋的方法則hitTest像這樣:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 

    UIView *result = nil; 
    for (UIView *child in self.subviews) 
    if ([child pointInside:point withEvent:event]) 
     if ((result = [child hitTest:point withEvent:event]) != nil) 
     break; 

    return result; 
} 
+0

雅我曾經使用過。無論如何,你能解釋一下代碼嗎? – 2013-05-03 06:47:35

+0

[hitTest:withEvent:](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html)「返回視圖層次結構中接收者的最遠後代(包括它本身)包含一個特定點。「 – 2013-05-03 07:10:11

1

我認爲你正在使用滾動視圖在這種情況下,一個subview.So你可以使用手勢,因爲我面對同樣的問題。

可以使用UITapGestureRecognizer這樣做......

-(void)viewDidLoad 
{ 
    UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
    gr.delegate = self; 
    [self.scrollView addGestureRecognizer:gr]; 
} 
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer 
{ 
    // do here whatever you wanna .......... 
} 
+0

我沒有使用viewcontroller – 2013-05-03 06:55:28

0

解決!

使用LongPressedGestureRecognizer。操作方法不斷被調用,直到用戶持有視圖。從這我可以計算出gestureRecognizer的狀態(UIGestureRecognizerStateBegan,...Ended,...Cancelled