检测2个子视图的触摸

时间:2013-08-17 22:53:53

标签: iphone ios objective-c xcode

我有一个UIView坐在UIScrollView上。

我希望能够在我的UIView捕获时正常滚动我的ScrollView:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

唯一的问题是我似乎无法让两个子视图同时检测到触摸。我可以将前一个设置为userInteractionEnabled为NO。但这并没有真正帮助我解决这两个问题。

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:-1)

在我的一个项目中,我在一个视图中捕获了触摸并以这种方式将方法传递给第二个:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    // Do something here

    [otherView touchesEnded:touches withEvent:event];
} 

编辑:您也可以使用第三个UIView来管理触摸。

将第三个UIView放在你的视图之上(它应该与你的屏幕一样宽)并让它来处理你的触摸

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    // Get touch position
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];

    // Use location to pass touches to one view ot the other

    if (location == something) {
        [oneView touchesEnded:touches withEvent:event];
    } else {
        [otherView touchesEnded:touches withEvent:event];
    }
}
相关问题