加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 在Mac OS X上的触控板中知道手指的位置

发布时间:2020-12-16 03:04:32 所属栏目:百科 来源:网络整理
导读:我正在开发一个Mac应用程序,我想知道手指在触控板上的位置. 有可能吗,如果是的话,怎么办? 解决方法 您的视图需要设置为接受触摸([self setAcceptsTouchEvents:YES]).当你得到一个触摸事件,如-touchesBeganWithEvent:,你可以通过查看它的normalizedPositio
我正在开发一个Mac应用程序,我想知道手指在触控板上的位置.

有可能吗,如果是的话,怎么办?

解决方法

您的视图需要设置为接受触摸([self setAcceptsTouchEvents:YES]).当你得到一个触摸事件,如-touchesBeganWithEvent:,你可以通过查看它的normalizedPosition(范围是[0.0,1.0] x [0.0,1.0]),根据它的deviceSize的大点来确定手指的位置(有72磅/英寸).触控板的左下角被视为零起点.

所以,例如:

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

(将此代码视为示例,而不是尝试和真实的,战斗的示例代码;我只是将其直接写入注释框中.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读