【问题标题】:Transparent UIView on top detecting touches顶部的透明 UIView 检测触摸
【发布时间】:2012-10-25 12:38:11
【问题描述】:

我有一个使用 UISplitViewController 构建的 iPad 项目:

  • RootViewController
  • DetailviewController

他们都在自己的类中使用手势识别器检测触摸。

我想在所有类之上创建一个透明的 UIView 以仅检测对角滑动(从左下角到右上角)。

因此,当检测到滑动时,我将启动一个函数否则没有附加任何内容,并且应该在低级视图上传递触摸。

我尝试了这两种解决方案:

  • 在此顶部透明视图上添加 GestureRecognizer,但这将隐藏对较低层次视图的所有触摸。(启用 userInteraction:当然是);

另一种解决方案是像这样进行初始化

-(id)initWithFrame:(CGRect)frame
{
   self = [super initWithFrame:frame];
  if (self) {
    // Initialization code
    [self setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.01]];
    [self setUserInteractionEnabled:NO];
 }

 return self;

}

并尝试检测滑动

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

但是此时所有的触摸都没有被检测到。

谁有好的解决方案?

【问题讨论】:

    标签: objective-c ios touch uigesturerecognizer transparent


    【解决方案1】:

    我不会像你提到的那样创建一个透明的 UIView。我将在UISplitViewController 的视图中添加UISwipeGestureRecognizer,这已经是包含所有子视图的视图。您可以访问 app 委托中的视图:

     UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    
     // attach the swipe gesture to the view that embeds the rootView and the detailView
     UISwipeGestureRecognizer* swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:splitViewController.view action:@selector(swipeUpdated:)];
    

    【讨论】:

    • Tnx @tiguero 快速回答。你是对的,但并非所有 IOS 版本都支持手势识别器。我发现正确的方法应该是子类化 UIWindow 并覆盖- (void)sendEvent:(UIEvent *)event-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event。所以我可以调用/不调用[super sendEvent:event] 来传递或不传递事件进入 UIViews 层次结构。你们觉得这个解决方案怎么样?
    【解决方案2】:

    你不能在UISplitViewController的视图中添加一个手势识别器吗?

    【讨论】:

      【解决方案3】:

      您应该查看Container Controllers。您可以创建自己的SplitViewController 并在检测到滑动的控制器顶部创建第三个视图。自定义容器控制器非常简单,并为您提供了很大的灵活性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-11
        • 1970-01-01
        • 2013-03-01
        • 2013-11-05
        相关资源
        最近更新 更多