如何阻止UIScrollView从其父视图获取触摸事件?

3
我有一个包含UIScrollView的UIView。 这个UIView应该响应一些触摸事件,但是由于它包含的scrollView,所以无法响应。
这个scrollView的contentView设置为OuterView中的MoviePlayer。 现在,每当我点击MoviePlayer所在区域时,我的OuterView触摸事件都不会响应。
我甚至将电影播放器的userInteraction设置为NO。
但现在似乎scrollView正在干扰。
我已经参考了SO上的这篇文章。 如何使父视图在任何子视图之前截取触摸序列? 但是那里的解决方案要求我将scrollView的userInteractionEnabled设置为NO。但如果我这样做,缩放也不会发生!
该怎么办?
1个回答

0

UIScrollView截取所有的触摸事件,因为它必须决定用户何时移动手指以滚动滚动视图。 您可能想要子类化UIView,然后在其中进行

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)evt;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)evt;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)evt;

方法是,检查所需的触摸,然后将所有这些方法转发到您的UIScrollViewInstance。例如:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface MyView: UIView {
    UIScrollView *scrollView;
}

@end

@implementation MyView

/* don't forget to alloc init your ScrollView init -init
   and release it in -dealloc! Then add it as a subview of self. */

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)evt
{
    /* check for a gesture */
    [scrollView touchesBegan:touches withEvent:evt];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)evt;
{
    /* check for a gesture */
    [scrollView touchesMoved:touches withEvent:evt];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)evt;
{
    /* check for a gesture */
    [scrollView touchesEnded:touches withEvent:evt];
}

@end

我一定会检查这个解决方案,但同时我正在尝试子类化ScrollView并覆盖hitTest并检查tapCount。 如果是1,则返回superview,否则返回self。 但是您能告诉我为什么tapCount为零吗? - Amogh Talpallikar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接