【问题标题】:iOS define touchable areas of an object, confine touches to subviews of selfiOS 定义对象的可触摸区域,将触摸限制在 self 的子视图中
【发布时间】:2013-03-05 11:29:27
【问题描述】:

我有一个子类 UIView,我将调用 customView。我希望启用触摸,以便用户可以操作具有手势识别器和其他控件的子视图,但我希望视图本身不可触摸,以便在视图下方绘制的视图仍然是可触摸的。换句话说,customView 将被绘制在应用程序中的其他视图之上,但我仍然希望下面的视图是可触摸的,同时允许触摸 customView 的子视图。

我尝试过这样使用 touchesBegan,但这不起作用。有任何想法吗?感谢阅读!

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];

//I've tagged the views that I want to be touchable.
    if ([touch view].tag == 1000 || [touch view].tag == 2000 || [touch view].tag == 3000) {
        self.userInteractionEnabled = YES;
    } else {
        self.userInteractionEnabled = NO;
    }
}

【问题讨论】:

  • 所以您希望 cusomView 不可触摸,并且 cusomview 下面的视图必须是可触摸的,对吧?
  • @Dilip 我想“屏蔽”/排除视图区域的触摸,某些子视图除外。

标签: ios uigesturerecognizer uitouch touchesbegan


【解决方案1】:

您需要做的是在您的 customView 中实现以下方法:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    //check if one of the subviews was hit, if so forward the touch event to it
    for (UIView *view in self.subviews){
        if (CGRectContainsPoint(view.frame, point))
            return view;
    }

    // use this to pass the 'touch' upward in case no subviews trigger the touch
    return [super hitTest:point withEvent:event];
}

【讨论】:

  • @Mrwolfy 你试过我的答案吗,我很想知道它是否对你有用,因为我有一个类似的问题,这种方法对我有用,但它与你的情况并不完全相同..
【解决方案2】:

然后将这些子视图添加到您的自定义视图中。另外,您的自定义视图不应该让它们触摸。并将自定义视图放在这些子视图下方,这样您选择的子视图就可以触摸到其他区域。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    相关资源
    最近更新 更多