2013-05-03 76 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