WP7中多点触控(三)

来源:互联网 发布:悖论 知乎 编辑:程序博客网 时间:2024/06/09 17:00
经过多点探索,实在没有办法,只有重回到silverlight低级触摸事件了。但是对GestureListener还有点依依不舍,毕竟那个操作起来方便。现在没办法,只有自己写,那就模拟一下吧。
现在重写上一篇那个实况方向控制盘。
先把界面做好:
  1. <Ellipse x:Name="innerRound" Width="100" Height="100" VerticalAlignment="Top"
  2.                      HorizontalAlignment="Left" Margin="100,350,0,0" Fill="Red"
  3.                      Stroke="Blue" StrokeThickness="5" >
  4.                 <Ellipse.RenderTransform>
  5.                     <TranslateTransform x:Name="innerTran"/>
  6.                 </Ellipse.RenderTransform>
  7.             </Ellipse> 
  8.             <TextBlock x:Name="textblock1" Width="150" Height="80" HorizontalAlignment="Left"
  9.                        VerticalAlignment="Top" Text="click Me" FontSize="40" Margin="300,0,0,0"/>
  10.             <TextBlock x:Name="direct" Text="direct" Width="200" Height="100" 
  11.                        HorizontalAlignment="Left"
  12.                        VerticalAlignment="Top"
  13.                        FontSize="40" Margin="250,200,0,0"/>
  14.             <TextBlock x:Name="click" Text="click" Width="150" Height="100" 
  15.                        HorizontalAlignment="Left"
  16.                        VerticalAlignment="Top"
  17.                        FontSize="40" Margin="300,300,0,0"/>
复制代码
界面就不截图了。和上一篇的界面基本一样,就是颜色不太一样。放置了两个Textblock来作为多点触控测试用。

然后在PageLoaded注册事件:
  1. Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);
复制代码
但是怎么模拟呢,毕竟这个是基础事件,获取到的是所有的触摸点,并且手指不一定第一次Down的时候就放在那个圆盘里面,可能是Move进去的。所有TouchPoint的Action事件有点难以判断了。而且,我不想用两个手指来控制我这个方向盘。但是,如果第一个进去的手指我又怎么判断它已经离开了这个方向盘呢。Action.Up?这个不行,毕竟可能是Move出去的。
另外,要模拟GestureListener,主要的是要得到的是水平和竖直方向的坐标改变。那么我们只能要先记录下上一个坐标。才能得到坐标的改变。

经过细想:现在制定的规则是:定义一个 Dictionary<int, Point>来保存TouchDevice.Id 和上一个点的Position。那么就可以用这个来判断是否是原来的手指并且可以得到坐标改变值。而且对每一个TouchPoint来判断它的Action是否是Up。如果是Up,那么判断下是否是刚才操作圆盘的手指,是的话就从dictionary里面Remove掉那个手指,只有就能让新的手指进来操作方向盘了。

添加两个成员变量:
  1.         Dictionary<int, Point> ETouchDict = new Dictionary<int, Point>();  

  2.         TranslateTransform tr = new TranslateTransform();
复制代码
具体代码如下:
  1. void Touch_FrameReported(object sender, TouchFrameEventArgs e)
  2.         {
  3.             TouchPoint primaryTouchPoint = e.GetPrimaryTouchPoint(null);

  4.             // Inhibit mouse promotion
  5.             if (primaryTouchPoint != null && primaryTouchPoint.Action == TouchAction.Down)
  6.                 e.SuspendMousePromotionUntilTouchUp();

  7.             TouchPointCollection touchPoints = e.GetTouchPoints(null);
  8.             foreach (var item in touchPoints)
  9.             {
  10.                 if (item.Action == TouchAction.Up)
  11.                 {
  12.                     if (ETouchDict.ContainsKey(item.TouchDevice.Id))
  13.                     {
  14.                         ETouchDict.Remove(item.TouchDevice.Id);
  15.                         this.innerTran.X = 0;
  16.                         this.innerTran.Y = 0;
  17.                         showDirect();
  18.                     }
  19.                 }
  20.                 if (item.TouchDevice.DirectlyOver == textblock1) {
  21.                     //Debug.WriteLine("textblock1 is touch!  "  + item.Action.ToString());
  22.                     click.Text = "A";
  23.                     click.Foreground = new SolidColorBrush(
  24.                             Color.FromArgb(255, (byte)rand.Next(256),
  25.                                                 (byte)rand.Next(256),
  26.                                                 (byte)rand.Next(256)));
  27.                 }
  28.                 else if (item.TouchDevice.DirectlyOver == innerRound)
  29.                 {
  30.                     //Debug.WriteLine("rectangle is touch!  " + item.Action.ToString());
  31.                     EllipseTouchHandle(item);
  32.                 }
  33.             }
  34.             
  35.         }

  36.         void EllipseTouchHandle(TouchPoint tp)
  37.         {
  38.             if (ETouchDict.Count() > 0 && !ETouchDict.ContainsKey(tp.TouchDevice.Id) )
  39.             {
  40.                 return;
  41.             }
  42.             if (ETouchDict.Count() <= 0)
  43.                 {
  44.                 ETouchDict.Add(tp.TouchDevice.Id, tp.Position);
  45.                 //Debug.WriteLine("Ellipse is touch!  x:" + tp.Position.X + " Y:" + tp.Position.Y);
  46.                 }
  47.             else
  48.             {
  49.                 Point LastPoint = ETouchDict.FirstOrDefault().Value;
  50.                 double horX = tp.Position.X - LastPoint.X;
  51.                 double verY = tp.Position.Y - LastPoint.Y;
  52.                 double x = this.innerTran.X + horX;
  53.                 double y = this.innerTran.Y + verY;
  54.                 if (x <= -50)
  55.                     this.innerTran.X = -50;
  56.                 else if (x >= 50)
  57.                     this.innerTran.X = 50;
  58.                 else
  59.                     this.innerTran.X = x;

  60.                 if (y <= -50)
  61.                     this.innerTran.Y = -50;
  62.                 else if (y >= 50)
  63.                     this.innerTran.Y = 50;
  64.                 else
  65.                     this.innerTran.Y = y;
  66.                 showDirect();
  67.                 //Debug.WriteLine("Ellipse is touch! HorizontalVelocity(X): " + horX.ToString() + " VerticalVelocity(Y):" + verY.ToString());
  68.                 ETouchDict.Remove(tp.TouchDevice.Id);
  69.                 ETouchDict.Add(tp.TouchDevice.Id, tp.Position);
  70.             }
  71.         }

  72.         void showDirect()
  73.         {
  74.             double x = innerTran.X;
  75.             double y = innerTran.Y;
  76.             if (x <= -15 && x >= -50 && y <= -15 && y >= -50 )
  77.             {
  78.                 direct.Text = "左上" + x + "," + y; 
  79.             }
  80.             else if (x >= 15 && x <= 50 && y <= -15 && y >= -50)
  81.             {
  82.                 direct.Text = "右上" + x + "," + y;
  83.             }
  84.             else if (x <= -15 && x >= -50 && y >= 15 && y <= 50)
  85.             {
  86.                 direct.Text = "左下" + x + "," + y;
  87.             }
  88.             else if (x >= 15 && x <= 50 && y >= 15 && y <= 50)
  89.             {
  90.                 direct.Text = "右下" + x + "," + y;
  91.             }
  92.             else if (x <= 15 && x >= -15 && y < 0)
  93.             {
  94.                 direct.Text = "上" + x + "," + y;
  95.             }
  96.             else if (x <= 15 && x >= -15 && y > 0)
  97.             {
  98.                 direct.Text = "下" + x + "," + y;
  99.             }
  100.             else if (y <= 15 && y >= -15 && x < 0)
  101.             {
  102.                 direct.Text = "左" + x + "," + y;
  103.             }
  104.             else if (y <= 15 && y >= -15 && x > 0)
  105.             {
  106.                 direct.Text = "右" + x + "," + y;
  107.             }
  108.             else
  109.             {
  110.                 direct.Text = "";
  111.             }
  112.         }
复制代码
这样,就可以多点触控了。而且,无论怎样操作,我的方向盘和那么的所谓的A键,就能发出“命令”了。
结果实在是不好截图。毕竟在真机上调试的。把工程放上来吧。http://115.com/file/c2p3tv39#MultiTouchDemo.zip

原文地址:http://blog.csdn.net/fengyun1989/article/details/7361429