简体   繁体   English

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

[英]Add functionality to standard Pan Gesture Recognizer in a UIScrollView

I am trying to keep track of where a finger is in a UIScrollView . 我试图跟踪手指在UIScrollView I have subclassed UIScrollView (see below) but unfortunately the gesture recognizer that I am adding is overriding the standard one. 我已经将UIScrollView子类化(见下文),但遗憾的是我添加的手势识别器正在覆盖标准的手势识别器。

As a result I get NSLog(@"Pan") to work but unfortunately the view doesn't scroll anymore. 结果我得到NSLog(@"Pan")工作但不幸的是视图不再滚动。

How can I get both gestures recognizers to work at the same time? 如何让两个手势识别器同时工作?

Thanks. 谢谢。

- (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");
}

If you want it not to override the standard one you just have to allow both to be simultaneously recognized. 如果你不想覆盖标准的那个,你只需要同时识别它们。

- (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");
}

EDIT : this method works! 编辑 :这种方法有效! You just need to set canCancelContentTouches as soon as possible (I do it in viewDidLoad ). 您只需要尽快设置canCancelContentTouches (我在viewDidLoad )。

ORIGINAL ANSWER : I have tried a new approach but unfortunately it doesn't fully work. 原始答案 :我尝试了一种新的方法,但不幸的是它没有完全发挥作用。

Instead of adding a gesture recognizer I am subclassing the UIScrollView and writing my own touchesBegan , touchesMoved , etc methods. 我没有添加手势识别器,而是touchesBegan UIScrollView并编写自己的touchesBegantouchesMoved等方法。

This way I know where the user is touching BUT unfortunately the PanGestureRecognizer is triggering touchesCancelled every time I start to scroll even after setting the canCancelContentTouches to NO . 这样我知道用户在哪里触摸但不幸的是,每当我canCancelContentTouches设置为NO后,每次开始滚动时touchesCancelled都会触发touchesCancelled

Does anybody know why? 有人知道为什么吗? I have also found this . 我也发现了这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM