iphone - HitTest 以避免 subview 但希望另一个 subview 工作

标签 iphone ios objective-c ipad cocoa-touch

我有一个只有灰色的背景 subview ,上面有可以拖动的图 block 。这一切都发生在一个 View Controller 中。问题是,当我进行 HitTest 并尝试避开 backgroundView 时,由于图 block 位于该 View 的顶部,它仍然看到那些坐标并避免触摸移动事件,因为我返回 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;
}

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

最佳答案

你应该有一个 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

关于iphone - HitTest 以避免 subview 但希望另一个 subview 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17055612/

相关文章:

iphone - Xcode:仅发布 iPhone 的应用程序更新?

iphone - OouraFFT 的输出有时正确,但有时完全错误。为什么?

objective-c - iBooks如何实现高亮文本功能?

iphone - iPhone 加速度计究竟测量什么?

iphone - 为 OBShapedButton 的(自定义形状按钮)图像着色并重新设置

ios - ios应用程序在 'load?'时做什么

ios - 如何在 iOS 上设置 UDP 的“不分段”标志?

c - Objective-C中的地板问题

objective-c - 在 Mac 应用程序和 Web 之间同步 'lot' 小块数据的最佳方法是什么?

iphone - 当函数返回 malloc() 的结果时,如何在 malloc() 之后 free() ?