我只是尝试在窗口中全局接收Touch事件,而不仅仅是在视图中。在我的代码中,你可以看到下面,我将获得触摸在魔术触控板中的绝对位置。只要光标在视图(红色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);
}
}发布于 2013-06-21 21:22:50
使用nil调用最后一个参数的touchesMatchingPhase:inView: (就像您正在做的那样)将会得到所有的触动。问题是,对于不在触摸区的控件,touchesBeganWithEvent:根本不会触发。
您可以使视图成为第一响应者,它将首先向其发送所有事件。请参阅becomeFirstResponder和Responder object
https://stackoverflow.com/questions/17235344
复制相似问题