在UIScrollView中为标准Pan Gesture Recognizer添加功能

ios*_*ard 6 uiscrollview uiview uigesturerecognizer ios

我想跟踪一个手指在哪里UIScrollView.我有子类UIScrollView(见下文)但不幸的是我添加的手势识别器超越了标准的手势识别器.

结果我开始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");
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*ski 14

如果你不想覆盖标准的那个,你只需要同时识别它们.

- (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");
}
Run Code Online (Sandbox Code Playgroud)


ios*_*ard 4

编辑:这个方法有效!canCancelContentTouches你只需要尽快设置(我是在 中设置的viewDidLoad)。

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

我没有添加手势识别器,而是对 、 等方法进行子类化并UIScrollView编写自己的方法。touchesBegantouchesMoved

这样我就知道用户触摸的位置,但不幸的是,即使将 设置为 NOtouchesCancelled后,每次我开始滚动时PanGestureRecognizer 都会触发。canCancelContentTouches

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