objective-c - osx - touchesBegan 没有结束也没有取消

标签 objective-c macos cocoa

问题是我在 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/

相关文章:

objective-c - 忍者我的应用程序 : What is FMNowAccountKey and how can i get it

iphone - 有没有办法从新的模态视图访问启动模态视图的 View Controller ?

具有 Accelerate 框架 vDSP 的 iPhone FFT

c - sendto 在 Mac OS X 10.8 上返回 EINVAL

objective-c - AVPlayerView Mac : How to disable all user interaction?

objective-c - 在 Cocoa 中使用表格作为布局容器?

针对互联网应用程序的 Objective-C/Cocoa 教程?

cocoa - 如何使我的 NSScroller 子类具有不同的宽度?

ios - 跨多个 UICollectionViewControllers 重用 UICollectionView

c++ - Mac 中带有 qt 的 DCMTK 库