UIScrollView子类从不接收用于滑动的touchesBegan消息

时间:2023-01-18 09:40:44

I am trying to make a scroll view only scrollable on a certain region. To do this, I am subclassing UIScrollView and overriding touchesBegan (similar to this question).

我试图使滚动视图只能在某个区域上滚动。为此,我将UIScrollView子类化并覆盖touchesBegan(类似于这个问题)。

Here's my (pretty simple) code.

这是我(非常简单)的代码。

.h

 @interface SuppressableScrollView : UIScrollView
 @end

.m

#import "SuppressableScrollView.h"

@implementation SuppressableScrollView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touchesBegan touches=%@ event=%@", touches, event);
    [super touchesBegan:touches withEvent:event];
}

@end

touchesBegan is only being called for touches that UIScrollView doesn't normally consume (like taps). Any idea how to intercept all of the touches?

touchesBegan仅被调用UIScrollView通常不会消耗的触摸(如点击)。知道怎么拦截所有的触摸?

I think I am missing a concept somewhere.

我想我在某个地方错过了一个概念。

2 个解决方案

#1


5  

I was recently looking into something similar for UITableViews. UITableView is and extension of UIScrollView. In digging around inside it I discovered that there are 4 gesture recognisers attached to the UIScrollView to pick up swipes and other things. I would suggest dump out the gesture recognisers properties to see if any are being automatically created (which I think they are). In which case the only option I can think of is to remove them, but then the scroll view will not respond to gestures.

我最近在寻找类似于UITableViews的东西。 UITableView是UIScrollView的扩展。在内部挖掘时,我发现有8个手势识别器连接到UIScrollView来拾取滑动和其他东西。我建议转出手势识别器属性,看看是否有自动创建(我认为它们是)。在这种情况下,我能想到的唯一选择是删除它们,但滚动视图不会响应手势。

So perhaps you need to look at those gesture recognisers and the gesture recogniser delegates you can use to see if there is a better place to hook into.

因此,您可能需要查看那些手势识别器和手势识别器代表,您可以使用它来查看是否有更好的地方可以连接。

P.S. gesture recognisers will automatically start swallowing events once they recogniser a gesture in progress.

附:一旦识别出正在进行的手势,手势识别器将自动开始吞咽事件。

#2


1  

If the frame size is greater than the content size, your touches began method may not fire.

如果帧大小大于内容大小,则触摸开始方法可能不会触发。

Since its working only for taps, my guess is that the content size of the scroll view is not set properly.

由于它只适用于水龙头,我的猜测是滚动视图的内容大小设置不正确。

#1


5  

I was recently looking into something similar for UITableViews. UITableView is and extension of UIScrollView. In digging around inside it I discovered that there are 4 gesture recognisers attached to the UIScrollView to pick up swipes and other things. I would suggest dump out the gesture recognisers properties to see if any are being automatically created (which I think they are). In which case the only option I can think of is to remove them, but then the scroll view will not respond to gestures.

我最近在寻找类似于UITableViews的东西。 UITableView是UIScrollView的扩展。在内部挖掘时,我发现有8个手势识别器连接到UIScrollView来拾取滑动和其他东西。我建议转出手势识别器属性,看看是否有自动创建(我认为它们是)。在这种情况下,我能想到的唯一选择是删除它们,但滚动视图不会响应手势。

So perhaps you need to look at those gesture recognisers and the gesture recogniser delegates you can use to see if there is a better place to hook into.

因此,您可能需要查看那些手势识别器和手势识别器代表,您可以使用它来查看是否有更好的地方可以连接。

P.S. gesture recognisers will automatically start swallowing events once they recogniser a gesture in progress.

附:一旦识别出正在进行的手势,手势识别器将自动开始吞咽事件。

#2


1  

If the frame size is greater than the content size, your touches began method may not fire.

如果帧大小大于内容大小,则触摸开始方法可能不会触发。

Since its working only for taps, my guess is that the content size of the scroll view is not set properly.

由于它只适用于水龙头,我的猜测是滚动视图的内容大小设置不正确。