1

我在 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)事件。

多点触控已启用。

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

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

4

3 回答 3

0

你有没有想过- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event改用?

于 2013-05-13T10:44:39.473 回答
0

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

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

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

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

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

于 2012-03-10T17:46:01.350 回答
0

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

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
于 2021-10-18T14:38:39.223 回答