【问题标题】:How to stop the UIScrollView get touch events from its parent view?如何停止 UIScrollView 从其父视图获取触摸事件?
【发布时间】:2012-01-30 12:01:39
【问题描述】:

我有一个包含UIScrollViewUIView。 这个UIView 应该响应一些触摸事件,但由于它包含的滚动视图,它没有响应。

此 scrollView 的 contentView 设置为 OuterView 内的 MoviePlayer。 现在,每当我单击 MoviePlayer 所在的区域时,我的 OuterView 的触摸事件都没有响应。

我什至将电影播放器​​的userInteraction 设置为NO

但现在 scrollView 似乎正在干扰它。

我已经在 SO 本身上提到了这篇文章。

How can a superview interecept a touch sequence before any of its subviews?

但是那里的解决方案要求我将滚动视图的userInteractionEnabled 设置为NO 但如果我这样做,我的捏缩放也不会发生!

怎么办?

【问题讨论】:

    标签: objective-c ios cocoa-touch


    【解决方案1】:

    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,则返回父视图,否则返回 self。但是你能告诉我为什么 tapCount 是零吗?
    猜你喜欢
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多