在UIScrollView中向标准Pan手势识别器添加功能

问题描述:

我试图跟踪UIScrollView中的手指位置。 我已经子类UIScrollView(见下文),但不幸的是我添加的手势识别器覆盖了标准手势识别器。在UIScrollView中向标准Pan手势识别器添加功能

因此,我得到NSLog(@"Pan")工作,但不幸的是,视图不再滚动。

我怎样才能得到这两个手势识别器在同一时间工作?

谢谢。

- (void)viewDidLoad:(BOOL)animated 
{ 
    [super viewDidLoad:animated]; 

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; 
    [scrollView addGestureRecognizer:panRecognizer]; 
} 


- (void)pan:(id)sender { 
    NSLog(@"Pan"); 
} 
+2

当两件事情同时发生时,你不会说你会期望发生什么。你期望它滚动*和*更新你的泛识别器?如果是这样,为什么不听滚动视图时调用的滚动视图委托方法? –

+0

我希望视图既可以滚动,也可以记录所有触及的点(我知道我可以通过'locationInView:'方法检索)。滚动视图委托听起来很有趣 - 我从来没有听说过...我对iOS编程颇为陌生 - 如何工作?谢谢。 –

+0

我发现[此滚动视图委托的引用](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intf/ UIScrollViewDelegate),但我不明白如何检索触摸的坐标。 –

编辑:这种方法可行!你只需要尽快设置canCancelContentTouches(我在viewDidLoad)。

原文答案:我尝试了一种新方法,但不幸的是它并没有完全奏效。

而不是增加一个手势识别器我将UIScrollView分类并编写我自己的touchesBegan,touchesMoved等方法。

这样,我知道用户触摸可惜PanGestureRecognizer甚至在canCancelContentTouches设置为NO之后每次我开始滚动时间触发touchesCancelled

有人知道为什么吗?我也发现了this

如果你不想重写标准的,你只需要同时识别两个标准。

- (void)viewDidLoad:(BOOL)animated 
{ 
    [super viewDidLoad:animated]; 

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; 
    panRecognizer.delegate = self; 
    [scrollView addGestureRecognizer:panRecognizer]; 
} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
{ 
    return TRUE; 
} 


- (void)pan:(id)sender { 
    NSLog(@"Pan"); 
} 
+0

这个作品完美! –