我在 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)事件。
多点触控已启用。
有人知道为什么会这样吗?当我使用这些事件删除子视图时,接收这些事件非常重要。
有解决办法吗?是否可以查询当前触摸的视图(或窗口)?