Iphone 重新发布触摸事件

Iphone 重新发布触摸事件,iphone,xcode,uitouch,uievent,Iphone,Xcode,Uitouch,Uievent,我正在尝试创建一个覆盖视图,该视图监视触摸,然后消失,但也将触摸事件转发到视图下方的任何位置 我的测试应用程序有一个视图,里面有一个按钮。我将覆盖视图添加为另一个子视图(本质上是按钮的同级视图),它占据了整个屏幕 对于我尝试过的两种解决方案,覆盖层都保持状态,以确定它对触摸的响应方式。当接收到ToucheSBegind事件时,覆盖将停止响应hitTest或pointInside,直到接收到touchesCancelled或touchesEnded - (UIView *)hitTest:(CGP

我正在尝试创建一个覆盖视图,该视图监视触摸,然后消失,但也将触摸事件转发到视图下方的任何位置

我的测试应用程序有一个视图,里面有一个按钮。我将覆盖视图添加为另一个子视图(本质上是按钮的同级视图),它占据了整个屏幕

对于我尝试过的两种解决方案,覆盖层都保持状态,以确定它对触摸的响应方式。当接收到ToucheSBegind事件时,覆盖将停止响应hitTest或pointInside,直到接收到touchesCancelled或touchesEnded

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if(_respondToTouch)
    {
        NSLog(@"Responding to hit test");
        return [super hitTest:point withEvent:event];
    }
    NSLog(@"Ignoring hit test");
    return nil;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if(_respondToTouch)
    {
        NSLog(@"Responding to point inside");
        return [super pointInside:point withEvent:event];
    }
    NSLog(@"Ignoring point inside");
    return NO;
}
对于第一种方法,我尝试重新发布事件:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if(!_respondToTouch)
    {
        NSLog(@"Ignoring touches began");
        return;
    }
    NSLog(@"Responding to touches began");
    _respondToTouch = NO;

    [[UIApplication sharedApplication] sendEvent:event];
}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches cancelled");
    _respondToTouch = YES;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches ended");
    _respondToTouch = YES;
}
但是,该按钮没有响应重新发布的事件

我的第二种方法是使用hitTest来发现覆盖下面的视图(我的按钮),然后直接向它发送touchesXXX消息:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches began");
    _respondToTouch = NO;
    UITouch* touch = [touches anyObject];
    _touchDelegate = [[UIApplication sharedApplication].keyWindow hitTest:[touch locationInView:self.superview] withEvent:event];
    CGPoint locationInView = [touch locationInView:_touchDelegate];
    NSLog(@"Sending touch %@ to view %@. location in view = %f, %f", touch, _touchDelegate, locationInView.x, locationInView.y);
    [_touchDelegate touchesBegan:touches withEvent:event];
}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches cancelled");
    [_touchDelegate touchesCancelled:touches withEvent:event];
    _respondToTouch = YES;
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches ended");
    [_touchDelegate touchesEnded:touches withEvent:event];
    _respondToTouch = YES;
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"Touches moved");
    [_touchDelegate touchesMoved:touches withEvent:event];
}
它找到了按钮(根据日志),但当我在按钮上调用touchesXXX时,按钮根本没有反应


我不确定还可以尝试什么,因为该按钮不会响应直接调用touchsbegind=/

可能对您有所帮助。您可能希望在覆盖视图截获原始触摸事件后10毫秒左右,使用计时器合成相同的触摸事件。不幸的是,该解决方案使用私有API调用,因此我无法使用它。