重新发布触摸事件

时间:2011-07-23 01:58:32

标签: iphone xcode uitouch uievent

我正在尝试创建一个覆盖视图来监控触摸然后消失,但也会将触摸事件转发到视图下方的任何内容。

我的测试应用程序有一个带有按钮的视图。我将叠加视图添加为另一个子视图(基本上是按钮的兄弟),它占据了整个屏幕。

对于我尝试的两种解决方案,叠加层保持状态以确定它如何响应触摸。收到touchesBegan事件后,叠加层将停止响应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时按钮根本没有反应。

我不确定还有什么可以尝试,因为按钮不会响应touchesBegan = /

的直接调用

0 个答案:

没有答案