前端每日三问#200502 html5手势检测原理是什么?

前言

http://qianduan.guru/2016/08/13/gesture_detection_in_html5/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io

随着 Hybrid 应用的丰富,HTML5 工程师们已经不满足于把桌面端体验简单移植到移动端,他们觊觎移动原生应用人性化的操作体验,特别是原生应用与生俱来的丰富的手势系统。HTML5 没有提供开箱即用的手势系统,但是提供了更底层一些的对 touch 事件的监听。基于此,我们可以做出自己的手势库。

手势

常用的 HTML5 手势可以分为两类,单点手势和两点手势。单点手势有 tap(单击),double tap(双击),long tap(长按),swipe(挥),move(移动)。两点手势有 pinch(缩放),rotate(旋转)。 接下来我们实现一个检测这些手势的 js 库,并利用这个手势库做出炫酷的交互效果。

最终效果

移动

关于移动手势检测我们在这篇博文中做过详细介绍,这里不再赘述。总结一下就是在每次touchmove事件发生时,把两个位移点之间的坐标位置相减,就可以了。

单击(tap)

手势检测的关键是用 touchstart,touchmove,touchend 三个事件对手势进行分解。那么怎么分解单击事件呢?

  1. 在 touchstart 发生时进入单击检测,只有一个接触点。因为单击事件限制为一个手指的动作。
  2. 没有发生 touchmove 事件或者 touchmove 在一个很小的范围(如下图)。限制 touchmove 在一个很小范围,是为了给用户一定的冗余空间,因为不能保证用户手指在接触屏幕的时候不发生轻微的位移。

单击位移限制.png

3 touchend 发生在 touchstart后的很短时间内(如下图)。这个时间段的阈值是毫秒级,用来限制手指和屏幕接触的时间。因为单击事件从开始到结束是很快的。

单击时间限制

有了上面的流程,就可以开始实现 tap 事件监测了。


  
  
  1. _getTime () {
  2. return new Date (). getTime ();
  3. }
  4. _onTouchStart ( e ) {
  5. //记录touch开始的位置
  6. this . startX = e . touches [ 0 ]. pageX ;
  7. this . startY = e . touches [ 0 ]. pageY ;
  8. if ( e . touches . length > 1 ) {
  9. //多点监测
  10. ...
  11. } else {
  12. //记录touch开始的时间
  13. this . startTime = this . _getTime ();
  14. }
  15. }
  16. _onTouchMove ( e ) {
  17. ...
  18. //记录手指移动的位置
  19. this . moveX = e . touches [ 0 ]. pageX ;
  20. this . moveY = e . touches [ 0 ]. pageY ;
  21. ...
  22. }
  23. _onTouchEnd ( e ) {
  24. let timestamp = this . _getTime ();
  25. if ( this . moveX !== null && Math . abs ( this . moveX - this . startX ) > 10 ||
  26. this . moveY !== null && Math . abs ( this . moveY - this . startY ) > 10 ) {
  27. ...
  28. } else {
  29. //手指移动的位移要小于10像素并且手指和屏幕的接触时间要短语500毫秒
  30. if ( timestamp - this . startTime < 500 ) {
  31. this . _emitEvent ( 'onTap' )
  32. }
  33. }
  34. }

双击(double tap)

和单击一样,双击事件也需要我们对手势进行量化分解。 1. 双击事件是一个手指的行为。所以在 touchstart 时,我们要判断此时屏幕有几个接触点。 2. 双击事件中包含两次独立的单击行为。理想情况下,这两次点击应该落在屏幕上的同一个点上。为了给用户一定的冗余空间,将两次点击的坐标点距离限制在10个像素以内。

双击位移限制3 双击事件本质是两次快速的单击。也即是说,两次点击的间隔时间很短。通过一定的测试量化后,我们把两次单击的时间间隔设为300毫秒。

双击时间限制.png

** 注意双击事件中我们检测了相邻两个 touchstart 事件的位移和时间间隔 **


  
  
  1. _onTouchStart ( e ) {
  2. if ( e . touches . length > 1 ) {
  3. ...
  4. } else {
  5. if ( this . previousTouchPoint ) {
  6. //两次相邻的touchstart之间距离要小于10,同时时间间隔小于300ms
  7. if ( Math . abs ( this . startX - this . previousTouchPoint . startX ) < 10 &&
  8. Math . abs ( this . startY - this . previousTouchPoint . startY ) < 10 &&
  9. Math . abs ( this . startTime - this . previousTouchTime ) < 300 ) {
  10. this . _emitEvent ( 'onDoubleTap' );
  11. }
  12. }
  13. //保存上一次touchstart的时间和位置信息
  14. this . previousTouchTime = this . startTime ;
  15. this . previousTouchPoint = {
  16. startX : this . startX ,
  17. startY : this . startY
  18. };
  19. }
  20. }

长按(long press)

长按应该是最容易分解的手势。我们可以这样分解:在 touchstart 发生后的很长一段时间内,如果没有发生 touchmove 或者 touchend 事件,那么就触发长按手势。

  1. 长按是一个手指的行为,需要检测屏幕上是否只有一个接触点。
  2. 如果手指在空间上发生了移动,那么长按事件取消。
  3. 如果手指在屏幕上停留的时间超过800ms,那么触发长按手势。
  4. 如果手指在屏幕上停留的时间小于800ms,也即 touchend 在 touchstart 发生后的800ms内触发,那么长按事件取消。

长按检测原理


  
  
  1. _onTouchStart ( e ) {
  2. clearTimeout ( this . longPressTimeout );
  3. if ( e . touches . length > 1 ) {
  4. } else {
  5. this . longPressTimeout = setTimeout (() => {
  6. this . _emitEvent ( 'onLongPress' );
  7. });
  8. }
  9. }
  10. _onTouchMove ( e ) {
  11. ...
  12. clearTimeout ( this . longPressTimeout );
  13. ...
  14. }
  15. _onTouchEnd ( e ) {
  16. ...
  17. clearTimeout ( this . longPressTimeout );
  18. ...
  19. }

缩放(pinch)

缩放是一个非常有趣的手势,还记得第一代iPhone双指缩放图片给你带来的震撼吗?虽然如此,缩放手势的检测却相对简单。

  1. 缩放是两个手指的行为,需要检测屏幕上是否有两个接触点。
  2. 缩放比例的量化,是通过两次缩放行为之间的距离的比值得到,如下图。

缩放** 所以缩放的核心是获取两个接触点之间的直线距离。**


  
  
  1. //勾股定理
  2. _getDistance ( xLen , yLen ) {
  3. return Math . sqrt ( xLen * xLen + yLen * yLen );
  4. }

这里的xLen是两个接触点x坐标差的绝对值,yLen相应的就是y坐标差的绝对值。


  
  
  1. _onTouchStart ( e ) {
  2. if ( e . touches . length > 1 ) {
  3. let point1 = e . touches [ 0 ];
  4. let point2 = e . touches [ 1 ];
  5. let xLen = Math . abs ( point2 . pageX - point1 . pageX );
  6. let yLen = Math . abs ( point2 . pageY - point1 . pageY );
  7. this . touchDistance = this . _getDistance ( xLen , yLen );
  8. } else {
  9. ...
  10. }
  11. }

在_onTouchStart函数中获取并且保存 touchstart 发生时两个接触点之间的距离。


  
  
  1. _onTouchMove ( e ) {
  2. if ( e . touches . length > 1 ) {
  3. let xLen = Math . abs ( e . touches [ 0 ]. pageX - e . touches [ 1 ]. pageX );
  4. let yLen = Math . abs ( e . touches [ 1 ]. pageY - e . touches [ 1 ]. pageY );
  5. let touchDistance = this . _getDistance ( xLen , yLen );
  6. if ( this . touchDistance ) {
  7. let pinchScale = touchDistance / this . touchDistance ;
  8. this . _emitEvent ( 'onPinch' ,{ scale : pinchScale - this . previousPinchScale });
  9. this . previousPinchScale = pinchScale ;
  10. }
  11. } else {
  12. ...
  13. }
  14. }

旋转(rotate)

旋转手势需要检测两个比较重要的值,一是旋转的角度,二是旋转的方向(顺时针或逆时针)。 其中旋转角度和方向的计算需要通过向量的计算来获取,本文不再展开,感兴趣的同学可以查看这里

旋转检测

首先,需要获取向量的旋转方向和角度


  
  
  1. //这两个方法属于向量计算,具体原理请阅读本文最后的参考文献
  2. _getRotateDirection ( vector1 , vector2 ) {
  3. return vector1 . x * vector2 . y - vector2 . x * vector1 . y ;
  4. }
  5. _getRotateAngle ( vector1 , vector2 ) {
  6. let direction = this . _getRotateDirection ( vector1 , vector2 );
  7. direction = direction > 0 ? - 1 : 1 ;
  8. let len1 = this . _getDistance ( vector1 . x , vector1 . y );
  9. let len2 = this . _getDistance ( vector2 . x , vector2 . y );
  10. let mr = len1 * len2 ;
  11. if ( mr === 0 ) return 0 ;
  12. let dot = vector1 . x * vector2 . x + vector1 . y * vector2 . y ;
  13. let r = dot / mr ;
  14. if ( r > 1 ) r = 1 ;
  15. if ( r < - 1 ) r = - 1 ;
  16. return Math . acos ( r ) * direction * 180 / Math . PI ;
  17. }

然后,我们在手指发生移动时,调用获取旋转方向和角度的方法。


  
  
  1. _onTouchStart ( e ) {
  2. ...
  3. if ( e . touches . length > 1 ) {
  4. this . touchVector = {
  5. x : point2 . pageX - this . startX ,
  6. y : point2 . pageY - this . startY
  7. };
  8. }
  9. ...
  10. }
  11. _onTouchMove ( e ) {
  12. ...
  13. if ( this . touchVector ) {
  14. let vector = {
  15. x : e . touches [ 1 ]. pageX - e . touches [ 0 ]. pageX ,
  16. y : e . touches [ 1 ]. pageY - e . touches [ 0 ]. pageY
  17. };
  18. let angle = this . _getRotateAngle ( vector , this . touchVector );
  19. this . _emitEvent ( 'onRotate' ,{
  20. angle
  21. });
  22. this . touchVector . x = vector . x ;
  23. this . touchVector . y = vector . y ;
  24. }
  25. ...
  26. }

实战

好了,我们的手势系统到这里就完成了。接下来要在实战中检验这套系统是否可靠,做一个简单的图片浏览器,支持图片缩放,旋转,移动,长按。 首先,做好dom规划,和[之前]一样,我们的事件监听机制并不直接作用在图片上,而是作用在图片的父组件上。

然后,可以开始使用上面的手势检测系统了。


  
  
  1. render () {
  2. return (
  3. < Gestures
  4. onPinch = { this . onPinch }
  5. onMove = { this . onMove }
  6. onRotate = { this . onRotate }
  7. onDoubleTap = { this . onDoubleTap }
  8. onLongPress = { this . onLongPress } >
  9. < div className = "wrapper" >
  10. < img src = "/images/gesture_detection_in_html5/2362670-f8b44d4b9101e8d6.jpg" />
  11. < /div >
  12. < /Gestures >
  13. );
  14. }

由于我们的手势系统检测的增量,因此不能直接把增量应用在对象上,而是需要把这些增量累加。以旋转为例:


  
  
  1. onRotate ( event ) {
  2. //对增量进行累加
  3. this . angle += event . angle
  4. this . setState ({
  5. angle : this . angle
  6. });
  7. }

至此,我们的手势检测就完成了:

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值