如何使用颤动在绘图中禁用多点触摸

时间:2018-08-28 04:59:26

标签: dart flutter

这个问题完全基于GestureDetector颤动。

例如: 在Application中,实现了GestureDetector类,因此默认情况下支持多点触控,现在我们需要禁用多点触控,那么什么是最好的方式呢?否则在使用GestureDetector中的flutter的绘图应用程序中会导致多点触控问题。

那么如何在手势检测器中禁用多点触摸?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但是我通过测量两个点之间的距离来解决。

The rule of how to measure the distance between two points

// Convert a rule to the code
double distanceBetweenTwoPoints(double x1,double y1 ,double x2, double y2){
double x = x1 - x2;
       x = x * x; 
double y = y1 - y2;
       y = y * y;

double result = x + y;
return sqrt(result);
}

首先,声明两个变量及其值

// These two variables are to save the previous points
var fingerPostionY = 0.0,fingerPostionX = 0.0;

然后在 onPanUpdate 方法中,我用了两个点来计算它们之间的距离。之后,我进行了比较,如果距离很大(例如50),则有很多手指,因此我忽略了它,否则它将只是屏幕上的一根手指。

onPanUpdate: (details) {
        if (fingerPostionY < 1.0){
          // assigen for the first time to compare
          fingerPostionY = details.globalPosition.dy;
          fingerPostionX = details.globalPosition.dx;
        }else{
          // they use a lot of fingers
          double distance = distanceBetweenTwoPoints(details.globalPosition.dx,details.globalPosition.dy,
                                                fingerPostionX,fingerPostionY);
          // the distance between two fingers must be above 50
          // to disable multi touch
          if(distance > 50)
            return;
          
          // update to use it in the next comparison
          fingerPostionY = details.globalPosition.dy;
          fingerPostionX = details.globalPosition.dx;
        } 

          // the code of drawing
        setState(() {
          RenderBox renderBox = context.findRenderObject();
          points.add(TouchPoints(
              points: renderBox.globalToLocal(details.globalPosition),
              paint: Paint()
                ..strokeCap = strokeType
                ..isAntiAlias = true
                ..color = activeColor.withOpacity(opacity)
                ..strokeWidth = strokeWidth));
        });
      },

重要说明:

  1. onPanEnd 方法中,您必须编写此行,因为它 表示手指已经向上

    fingerPostionY = 0.0;

  2. 在绘图代码中仍然存在一些尚未解决的性能问题