知道Mac OS X下手指在触控板中的位置 [英] know the position of the finger in the trackpad under Mac OS X

查看:313
本文介绍了知道Mac OS X下手指在触控板中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Mac应用程序,当有触摸时,我想知道手指在触控板中的位置。

I am developing an Mac application and I would like to know the position of the finger in the trackpad when there is a touch.

这是可能的,如果是的,如何?

Is it something possible and if yes, how?

推荐答案

您的视图需要设置为接受触摸( [self setAcceptsTouchEvents:YES ] )。当您得到 -touchesBeganWithEvent:的触摸事件时,您可以通过查看其 normalizedPosition 来找出手指的位置, (每英寸72bp),它的范围是[0.0,1.0] x [0.0,1.0]),因为它 deviceSize 。触控板的左下角被视为零原点。

Your view needs to be set to accept touches ([self setAcceptsTouchEvents:YES]). When you get a touch event like -touchesBeganWithEvent:, you can figure out where the finger lies by looking at its normalizedPosition (range is [0.0, 1.0] x [0.0, 1.0]) in light of its deviceSize in big points (there are 72 bp per inch). The lower-left corner of the trackpad is treated as the zero origin.

因此,例如:

- (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);
   }
}

(将此代码视为插图,和真正的,战斗的示例代码;我只是直接写入评论框。)

(Treat this code as an illustration, not as tried and true, battle-worn sample code; I just wrote it directly into the comment box.)

这篇关于知道Mac OS X下手指在触控板中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆