简体   繁体   English

命中测试以避免子视图,但希望另一个子视图起作用

[英]hit test to avoid subview but want another subviews to work

I have a background subview which just have grey color's, and got tiles on top of it which can be dragged. 我有一个背景子视图,它只有灰色,并且在它上面有可以拖动的图块。 This is all happening in one view controller. 所有这些都在一个视图控制器中发生。 The problem is that when I do a hit test and try to avoid the backgroundView, since the tiles are on top of that view, it still see's those coordinates and avoid the touches move event since I am returning nil. 问题是,当我执行命中测试并尝试避免使用backgroundView时,由于图块位于该视图的顶部,因此它仍会看到这些坐标,并且由于我返回nil而避免了touches move事件。

Below is the code: 下面是代码:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    for (UIView *view in self.subviews) {

        //This checks if touches are within the region of the custom view based on its cordinates.
        if (CGRectContainsPoint(view.frame, point)) {
            NSLog(@"touched the boat view %f %f and %f %f",view.frame.origin.x, view.frame.origin.y, point.x, point.y);

            if ([view isEqual:backgroundView])  {
                return nil;
            }
            else
                return view;
        }
    }

    return nil;
}

If you can see I am verifying for the background view, but since my tiles are on of the background view, those are not being dragged based on my other logic. 如果您看到我正在验证背景视图,但是由于我的图块位于背景视图中,因此不会根据我的其他逻辑来拖动这些图块。 I have tried userInteractiveEnabled to No, but that doesn't seams to work. 我尝试将userInteractiveEnabled设置为No,但这无法正常工作。 Any suggestions? 有什么建议么?

you should have a subclass of uiview and implement hitTest on this subclass : 您应该具有uiview的子类,并在此子类上实现hitTest:

@implementation EDPassTroughView

- (id)initWithFrame:(CGRect)frame {
   self = [super initWithFrame:frame];
   if (self) {
    // Initialization code
   }
   return self;
}

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
   UIView *hitView = [super hitTest:point withEvent:event];
   if (hitView == self)
      return nil;
   else
      return hitView;
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM