ビューだけでなく、ウィンドウでグローバルにタッチイベントを受信しようとしています。私のコードでは、以下に示すように、魔法のトラックパッドでタッチの絶対位置を取得します。カーソルがビュー (赤い NSRect) 内にある限り正常に動作しますが、このビューの外でタッチを受け取るにはどうすればよいですか? 多くのコミュニティとアップル開発センターで解決策を探しましたが、何も見つかりませんでした。問題はこれだと思います: NSSet *touches = [ev touchesMatchingPhase:NSTouchPhaseTouching inView:nil];
NSEvent にすべてのタッチを取得するメソッドはありませんか?
誰かが私を助けてくれることを願っています。
ここで私の実装:
@implementation MyView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
[self setAcceptsTouchEvents:YES];
myColor = [NSColor colorWithDeviceRed:1.0 green:0 blue:0 alpha:0.5];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.
NSRect bounds = [self bounds];
[myColor set];
[NSBezierPath fillRect:bounds];
}
- (void)touchesBeganWithEvent:(NSEvent *)ev {
NSSet *touches = [ev touchesMatchingPhase:NSTouchPhaseTouching inView:nil];
for (NSTouch *touch in touches) {
NSPoint fraction = touch.normalizedPosition;
NSSize whole = touch.deviceSize;
NSPoint wholeInches = {whole.width / 72.0, whole.height / 72.0};
NSPoint pos = wholeInches;
pos.x *= fraction.x;
pos.y *= fraction.y;
NSLog(@"%s: Finger is touching %g inches right and %g inches up "
@"from lower left corner of trackpad.", __func__, pos.x, pos.y);
}
}