【问题标题】:check if a uiView has handled touch检查 uiView 是否已处理触摸
【发布时间】:2016-04-08 00:21:15
【问题描述】:

我想要做的是将UIView 放在另一个UIView 之上,它们都是屏幕大小。顶部UIView 包含许多 cocos 节点,当我触摸它们时会响应。但是当我触摸一个没有 cocos 节点的地方时,底部的UIView 应该会响应。 我不知道该怎么做。我的想象是检查顶部 uiView 是否处理触摸,什么都不做。否则让底部UIView 开始响应。但我不知道如何检查。我只知道如何检查触摸,但似乎UIView 在我触摸一些它无法处理的地方时也会被触摸。

【问题讨论】:

  • 你可以通过一个 UIView 来实现它,它上面有其他 UIViews。一开始不需要 2 个 UIView 作为核心。
  • 谢谢!我尝试将顶视图添加为底视图的subView,但我仍然无法触摸底视图。可能是因为顶视图中有一些不可见的层来处理触摸并阻止它?

标签: objective-c iphone uiview cocos2d-iphone touch


【解决方案1】:

我认为您不需要 2 个视图。

只取一个视图并将所有节点添加到该视图。

在.h中

IBOutlet UIView *bgView;//your view

UITapGestureRecognizer *viewTapRecognizer;// view tap recognizer

在.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    viewTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelGesture:)];
    [bgView addGestureRecognizer:viewTapRecognizer];

    for (UIView *subView in [bgView subviews]) {

        UITapGestureRecognizer *nodeTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelGesture:)];

        [subView addGestureRecognizer:nodeTapRecognizer];
    }
}

- (void)handelGesture:(UITapGestureRecognizer*)sender {

    if (sender == viewTapRecognizer) {
        // your view is tapped
        NSLog(@"........Tapped view..........");
    }
    else {
        // your node is tapped
        NSLog(@"........Tapped node..........");
    }
}

试试这个。它可能对你有用。

【讨论】:

  • @AndrewRomanov 请先检查代码我为每个节点添加 UITapGestureRecognizer。所以请在投票前检查一下。
【解决方案2】:

您可以通过两种方式实现它:使用 pointInside... 方法或 hitTest... 方法。
如果您将使用 pointInside... 您只能使用您的视图(顶视图和底视图)。您将使用顶视图继承 UIView 并覆盖 pointInside... 方法。如果用户点击可可节点,您将返回 YES,否则返回 NO

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [self didUserTapCocoaNode:point];
    return inside;
}

如果你想要更复杂的逻辑,你应该使用第三个视图,UIView 的子类(容器视图),它将包含顶视图和底视图。覆盖方法 hitTest...,并返回底部或顶部视图。

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event  
 {
    UIView* result = nil;
    if ([self touchedCocoaNode:point])
    {
      result = self.topView;
    }
    else
    {
      result = self.bottomView;
    } 

    return result;
 }

UPD didUserTapCocoaNode 的示例实现与伪代码:

 - (BOOL)didUserTapCocoaNode:(CGPoint)pointInSelf
{
  __block BOOL tappedSomeNode = NO;
  [self.nodes enumerateObjectsUsingBlock:^(NodeType* obj, NSInteger idx, BOOL* stop){
     CGPoint pointInNode = [obj convertPoint:pointInSelf fromView:self];
     tappedSomeNode = [obj pointInside:pointInNode withEvent:nil];
     *stop = tappedSomeNode;
   }]
  return tappedSomeNode;
}

【讨论】:

  • 感谢您的代码。但是如何查看didUserTapCocoaNode
  • 我已经用伪代码示例更新了我的答案。主要思想是枚举所有节点,而您没有找到接触的节点。 p.s.什么是可可节点,它是 UIView 的子类还是一些抽象对象?
猜你喜欢
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 2011-02-17
  • 2023-03-08
  • 1970-01-01
  • 2010-12-02
  • 2011-03-23
  • 2016-07-28
相关资源
最近更新 更多