Objective c 在Mac OS X下知道手指在轨迹板中的位置
我正在开发一个Mac应用程序,我想知道触摸板上手指的位置Objective c 在Mac OS X下知道手指在轨迹板中的位置,objective-c,cocoa,macos,trackpad,Objective C,Cocoa,Macos,Trackpad,我正在开发一个Mac应用程序,我想知道触摸板上手指的位置 有可能吗?如果有,怎么做?我不知道是否有ObjC接口,但您可能会发现C很有趣。在Cocoa(Obj-C级别)尝试以下方法-尽管记住许多用户仍在使用鼠标控制 您的视图需要设置为接受触摸([self-setAcceptsTouchEvents:YES])。当你得到一个触摸事件,如-touchesBeganWithEvent:,你可以通过查看手指的标准化位置(范围是[0.0,1.0]x[0.0,1.0]),根据其设备的大小来判断手指的位置(每英
有可能吗?如果有,怎么做?我不知道是否有ObjC接口,但您可能会发现C很有趣。在Cocoa(Obj-C级别)尝试以下方法-尽管记住许多用户仍在使用鼠标控制
您的视图需要设置为接受触摸(
[self-setAcceptsTouchEvents:YES]
)。当你得到一个触摸事件,如-touchesBeganWithEvent:
,你可以通过查看手指的标准化位置(范围是[0.0,1.0]x[0.0,1.0]),根据其设备的大小来判断手指的位置(每英寸有72 bp)。轨迹板的左下角被视为零原点
例如:
- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (!self) return nil;
/* You need to set this to receive any touch event messages. */
[self setAcceptsTouchEvents:YES];
/* You only need to set this if you actually want resting touches.
* If you don't, a touch will "end" when it starts resting and
* "begin" again if it starts moving again. */
[self setWantsRestingTouches:YES]
return self;
}
/* One of many touch event handling methods. */
- (void)touchesBeganWithEvent:(NSEvent *)ev {
NSSet *touches = [ev touchesMatchingPhase:NSTouchPhaseBegan inView:self];
for (NSTouch *touch in touches) {
/* Once you have a touch, getting the position is dead simple. */
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);
}
}
(将此代码视为一个示例,而不是久经考验的真实示例代码;我只是将其直接写到注释框中。)Swift 3:
我已经为NSTouch编写了一个扩展,它返回相对于NSView的触摸板触摸位置:
extension NSTouch {
/**
* Returns the relative position of the touch to the view
* NOTE: the normalizedTouch is the relative location on the trackpad. values range from 0-1. And are y-flipped
* TODO: debug if the touch area is working with a rect with a green stroke
*/
func pos(_ view:NSView) -> CGPoint{
let w = view.frame.size.width
let h = view.frame.size.height
let touchPos:CGPoint = CGPoint(self.normalizedPosition.x,1 + (self.normalizedPosition.y * -1))/*flip the touch coordinates*/
let deviceSize:CGSize = self.deviceSize
let deviceRatio:CGFloat = deviceSize.width/deviceSize.height/*find the ratio of the device*/
let viewRatio:CGFloat = w/h
var touchArea:CGSize = CGSize(w,h)
/*Uniform-shrink the device to the view frame*/
if(deviceRatio > viewRatio){/*device is wider than view*/
touchArea.height = h/viewRatio
touchArea.width = w
}else if(deviceRatio < viewRatio){/*view is wider than device*/
touchArea.height = h
touchArea.width = w/deviceRatio
}/*else ratios are the same*/
let touchAreaPos:CGPoint = CGPoint((w - touchArea.width)/2,(h - touchArea.height)/2)/*we center the touchArea to the View*/
return CGPoint(touchPos.x * touchArea.width,touchPos.y * touchArea.height) + touchAreaPos
}
}
扩展NSTouch{
/**
*返回触摸到视图的相对位置
*注意:normalizedTouch是轨迹板上的相对位置。值的范围为0-1。并且是y翻转的
*TODO:如果触摸区域使用绿色笔划的矩形,则进行调试
*/
func pos(u-view:NSView)->CGPoint{
设w=视图.框架.尺寸.宽度
设h=view.frame.size.height
让touchPos:CGPoint=CGPoint(self.normalizedPosition.x,1+(self.normalizedPosition.y*-1))/*翻转触摸坐标*/
让deviceSize:CGSize=self.deviceSize
让deviceRatio:CGFloat=deviceSize.width/deviceSize.height/*找到设备的比率*/
让视图比率:CGFloat=w/h
变量接触面积:CGSize=CGSize(w,h)
/*均匀地将设备收缩到视图框*/
如果(deviceRatio>viewRatio){/*设备比视图宽*/
touchArea.height=h/viewRatio
touchArea.width=w
}否则,如果(deviceRatio
这是我写的一篇关于macOS中的GestureHUD类的文章。还有一个现成扩展的链接:
例子:
这似乎是一个非常有趣的话题。我需要看看,迪珀。谢谢