2011-08-04 94 views
0

我使用uviewview父视图.... overidded触摸开始并触摸结束方法... 当我在视图中添加一个UIButton作为子视图并触摸按钮触地事件被检测到并且相关联的方法被调用...但是被触发的touchbegan方法不被称为...当按下按钮时在UIButton下检测uiview上的触摸

我想要的是当我触摸按钮时触发事件和touchBegan方法关联的方法的uiview都被同时调用... UIbutton没有通过触摸它的超级视图,即uiview .....? 任何想法如何做到这一点? 感谢

回答

1

我不知道究竟如何同时调用两个touchesBegan事件的两种不同的意见,但你可能要重写的UIViewhitTest:withEvent:方法。这将允许你捕捉之下的视图UIButton之前它到达按钮(hitTests工作从window向上到最前面的视图)。

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    if (passToSubviews) 
    { 
     return [super hitTest: point withEvent: event]; 
    } 

    // otherwise stop the subview receiving touches 
    if ([super hitTest: point withEvent: event]) 
    { 
     return self; 
    } 

    return nil;  
} 

也许这可以帮助...

编辑:

只是一个猜测,但你可以尝试:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    UIView* hitSubview = [super hitTest: point withEvent: event]; 
    if (hitSubview) 
    { 
     [self doTouchedInStuff]; 
     return hitSubview; 
    } 

    return self;   
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self doTouchedInStuff]; 

    [super touchesBegan:touches withEvent:event]; 
} 
+0

passToSubview?只是重写这个方法会有所作为? – Tornado

+0

是的,这将允许您定义检测到触摸的位置,但不能同时在2个不同视图上检测触摸 – Sam

+0

全部完成....非常感谢 – Tornado