问题是我在 OS X 10.11 上使用触控板。要重现我的问题,创建简单的 Cocoa 应用程序并将以下代码添加到 ViewController 就足够了:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setAcceptsTouchEvents:YES];
}
-(void) touchesBeganWithEvent:(NSEvent *)event {
NSLog(@"touchesBeganWithEvent");
}
-(void) touchesCancelledWithEvent:(NSEvent *)event {
NSLog(@"touchesCancelledWithEvent");
}
-(void) touchesEndedWithEvent:(NSEvent *)event {
NSLog(@"touchesEndedWithEvent");
}
启动后只需用 3 或 4 根手指滑动即可更改桌面,您将看到 2 或 3 条消息:touchesBeganWithEvent。就是这样。我需要追踪触摸,但应用程序将充满刚刚开始但从未结束的触摸。
为什么触摸没有被取消或结束?有什么我想念的吗?
最佳答案
所以,这似乎是某种奇怪的行为,我看到的唯一方法是在 touchesBeganWithEvent:
中获取所有事件的触摸,并与您存储的触摸进行比较以删除取消的触摸。
- (void)touchesBeganWithEvent:(NSEvent *) theEvent
{
NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseAny inView:nil];
NSMutableArray* touchesToRemove = [NSMutableArray arrayWithCapacity:5];
for (NSTouch* storedTouch in storedTouches) {
for (NSTouch* touch in touches) {
if ([[touch identity] isEqual:[storedTouch identity]])
[touchesToRemove addObject:storedTouch];
}
}
for (NSTouch* touch in touchesToRemove) {
[storedTouches removeObject:touch];
}
// process new touches as always
}
一些伪代码来显示解决方法。
关于objective-c - osx - touchesBegan 没有结束也没有取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33945238/