【问题标题】:touchesEnded occasionally not calledtouchesEnded 偶尔不调用
【发布时间】:2012-03-10 07:08:20
【问题描述】:

我在 UIView 中实现了以下事件处理程序:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {     
        self.multipleTouchEnabled = YES;
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSArray* touchObjects = [[event allTouches] allObjects];
    for(int i=0; i<[touchObjects count]; i++) 
    {
        touch = (UITouch*)[touchObjects objectAtIndex:i];
        curPoint = [touch locationInView:self];
        NSLog([NSString stringWithFormat:@"begin = %f,%f",curPoint.x,curPoint.y]);
    }    
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint curPoint;
    for (UITouch *touch in touches) 
    {
        curPoint = [touch locationInView:self];
        NSLog([NSString stringWithFormat:@"ended = %f,%f",curPoint.x,curPoint.y]);
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
     //logging touches
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
     //logging touches
}

问题是我偶尔不会收到“touchesEnded”(或touchesCancelled)事件。我总是收到“touchesBegan”和“touchedMoved”事件。

我已经登录了这些方法,因此我 100% 确定我偶尔不会收到我期待的“touchesEnded”(或 touchesCancelled)事件。

多点触控已启用。

有人知道为什么会这样吗?当我使用这些事件删除子视图时,接收这些事件非常重要。

有解决办法吗?是否可以查询当前触摸的视图(或窗口)?

【问题讨论】:

  • 如果您向我们展示您的实际源代码会更好,因为它可能包含您的假源代码没有的问题。
  • 更新了代码。真的就这么简单。顺便说一句:iPhone 4,iOS 5.0.1。多次触摸更容易重现,但偶尔一次触摸仍会发生。
  • 是否有查询 UIView、UIWindow 或其他当前触摸的方法?

标签: ios objective-c uiview touches


【解决方案1】:

您的touchesBegan:withEvent: 方法不正确。它是这样说的:

NSArray* touchObjects = [[event allTouches] allObjects];

问题是[event allTouches] 返回所有触摸,而不仅仅是开始阶段的触摸。因此,当第一根手指触摸屏幕时,您将记录一条begin = %f,%f 消息。然后当第二根手指触摸屏幕时,您将记录两条 begin = %f,%f 消息,其中一条是您之前见过的触摸。

您的 touchesEnded:withEvent: 方法仅正确枚举了 touches 参数,该参数仅包含结束阶段的触摸。因此,对于两指触摸,您将记录三个“开始”消息,但只有两个“结束”消息。

我不知道有什么方法可以在触摸处理方法之外询问当前的触摸。

【讨论】:

  • 我会尝试切换它并让你知道我看到了什么。谢谢。
  • 还是一样的行为。我经常没有收到相应的 touchesBegan 的 touchesEnded 或 touchesCancelled。它可以很简单。我触摸屏幕,它记录了开始触摸,我抬起手指,不调用 touchesEnded。多次触摸它的频率要高得多,但即使是一次触摸也可能发生。我绝对必须知道用户何时不再触摸屏幕。手势识别器代码是否更稳定?如果我的视图没有收到这些事件,这些事件会去哪里?
  • 我知道我不是唯一看到这个的人,一个简单的谷歌会告诉你这个。如果我无法通过查询获得当前的触摸,有没有办法告诉系统重新评估触摸并重新发送它们?
  • 框架是否有可能因为它们“陈旧”而没有发送它们?如果是这样,是否可以告诉系统发送事件,即使时间戳太旧?
  • 我也遇到了同样的问题,请帮助,这真令人气愤。我还需要知道什么时候不再有触摸,并且保持计数对于这种行为根本不可靠
【解决方案2】:

您是否想过改用- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

【讨论】:

    【解决方案3】:

    我有同样的问题,添加 shouldBegin 委托解决了这个问题

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多