UIView与UIViewController中触摸事件处理的区别

4
所以,我正在捕获多个触摸,并确定每个触摸发生的数量和位置,我在UIView UIViewController之间看到不同的行为。
基本上,事件处理程序如下所示:(在这种情况下是touchesEnded,但实际上并不重要)。在View中发生的情况是,我得到了整个触摸集合,但在控制器中,我一次只能得到一个触摸,即从未有过触摸集合,但是TouchesEnded会为同时发生的每个触摸执行。
我尽可能相同地添加子视图...... 有人能向我解释发生了什么吗?为什么控制器版本没有给我集合?
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ 
    int total = 0;

    for (UITouch *touch in touches) 
    {
        for(UIView *myView in [contentView subviews])
        {
            if (CGRectContainsPoint([myView frame], [touch locationInView:self.view]))
                total++;  // this is always 1 for the UIViewController
            // but I get the full number within the UIView version
        }
    }
}
2个回答

0

我怀疑您获取了触摸事件,但视图不同。检查contentView是否相同并具有相同的子视图。


0

我不确定我是对的,但这个方法总是对我有效。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ 
          UITouch *touch = [touches anyObject];
         If(touch.view==your view)
         {
                NSLog("do your task here");
         }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接