1

我有一个只有灰色的背景子视图,上面有可以拖动的瓷砖。这一切都发生在一个视图控制器中。问题是,当我进行命中测试并尝试避免使用 backgroundView 时,由于图块位于该视图的顶部,它仍然会看到那些坐标并避免触摸移动事件,因为我返回 nil。

下面是代码:

- (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;
}

如果您可以看到我正在验证背景视图,但由于我的图块位于背景视图中,因此不会根据我的其他逻辑拖动这些图块。我已经尝试将 userInteractiveEnabled 设置为否,但这并不能正常工作。有什么建议么?

4

1 回答 1

1

you should have a subclass of uiview and implement hitTest on this subclass :

@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
于 2013-06-11T23:53:48.413 回答