iphone - 确定是否在uiview的子视图中发生了触摸

sol*_*sol 9 iphone events touch subview uiview

在UIView的子类中,我有:

    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
       if(touch occurred in a subview){
         return YES;
       }

       return NO;
    }
Run Code Online (Sandbox Code Playgroud)

我可以在if语句中加入什么?我想检测子视图中是否发生了触摸,无论它是否位于UIView的框架内.

Chr*_*orr 12

试试看:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    return CGRectContainsPoint(subview.frame, point);
}
Run Code Online (Sandbox Code Playgroud)

如果要在实现此方法的视图内部进行触摸,则返回YES,请使用以下代码:(如果要将手势识别器添加到位于容器边界外的子视图中)

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if ([super pointInside:point withEvent:event])
    {
        return YES;
    }
    else
    {
        return CGRectContainsPoint(subview.frame, point);
    }
}
Run Code Online (Sandbox Code Playgroud)


Bra*_*Guy 2

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
   return ([self hitTest:point withEvent:nil] == yourSubclass)
}
Run Code Online (Sandbox Code Playgroud)

方法 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 返回包含指定点的视图层次结构(包括其自身)中接收器的最远后代。我所做的就是返回最远视图与子视图的比较结果。如果您的子视图也有子视图,这可能不适合您。所以在这种情况下你想要做的是:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
   return ([[self hitTest:point withEvent:nil] isDescendantOfView:yourSubclass])
}
Run Code Online (Sandbox Code Playgroud)