微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WP7中多点触控三

经过多点探索,实在没有办法,只有重回到silverlight低级触摸事件了。但是对GestureListener还有点依依不舍,毕竟那个操作起来方便。现在没办法,只有自己写,那就模拟一下吧。
现在重写上一篇那个实况方向控制盘。
先把界面做好:
  1. <Ellipse x:Name="innerRound" Width="100" Height="100" VerticalAlignment="Top"
  2.                      HorizontalAlignment="Left" Margin="100,350,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"/>
  10.             <TextBlock x:Name="direct" Text="direct" Width="200" Height="100" 
  11.                        HorizontalAlignment="Left"
  12.                        VerticalAlignment="Top"
  13.                        FontSize="40" Margin="250,200,serif; font-size:12px; line-height:1.8em">             <TextBlock x:Name="click" Text="click" Width="150" Height="100" 
  14.                        FontSize="40" Margin="300,300,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),serif; font-size:12px; line-height:1.8em">                                                 (byte)rand.Next(256)));
  26.                 else if (item.TouchDevice.DirectlyOver == innerRound)
  27.                     //Debug.WriteLine("rectangle is touch!  " + item.Action.ToString());
  28.                     EllipsetouchHandle(item);
  29.             }
  30.             
  31.         }
  32.         void EllipsetouchHandle(TouchPoint tp)
  33.             if (etouchDict.Count() > 0 && !etouchDict.ContainsKey(tp.TouchDevice.Id) )
  34.                 return;
  35.             if (etouchDict.Count() <= 0)
  36.                 {
  37.                 etouchDict.Add(tp.TouchDevice.Id,tp.Position);
  38.                 //Debug.WriteLine("Ellipse is touch!  x:" + tp.Position.X + " Y:" + tp.Position.Y);
  39.                 }
  40.             else
  41.                 Point LastPoint = etouchDict.FirstOrDefault().Value;
  42.                 double horX = tp.Position.X - LastPoint.X;
  43.                 double verY = tp.Position.Y - LastPoint.Y;
  44.                 double x = this.innerTran.X + horX;
  45.                 double y = this.innerTran.Y + verY;
  46.                 if (x <= -50)
  47.                     this.innerTran.X = -50;
  48.                 else if (x >= 50)
  49.                     this.innerTran.X = 50;
  50.                 else
  51.                     this.innerTran.X = x;
  52.                 if (y <= -50)
  53.                     this.innerTran.Y = -50;
  54.                 else if (y >= 50)
  55.                     this.innerTran.Y = 50;
  56.                     this.innerTran.Y = y;
  57.                 showDirect();
  58.                 //Debug.WriteLine("Ellipse is touch! HorizontalVeLocity(X): " + horX.ToString() + " VerticalVeLocity(Y):" + verY.ToString());
  59.                 etouchDict.Remove(tp.TouchDevice.Id);
  60.         void showDirect()
  61.             double x = innerTran.X;
  62.             double y = innerTran.Y;
  63.             if (x <= -15 && x >= -50 && y <= -15 && y >= -50 )
  64.                 direct.Text = "左上" + x + "," + y; 
  65.             else if (x >= 15 && x <= 50 && y <= -15 && y >= -50)
  66.                 direct.Text = "右上" + x + "," + y;
  67.             else if (x <= -15 && x >= -50 && y >= 15 && y <= 50)
  68.                 direct.Text = "左下" + x + ",serif; font-size:12px; line-height:1.8em">             else if (x >= 15 && x <= 50 && y >= 15 && y <= 50)
  69.                 direct.Text = "右下" + x + ",serif; font-size:12px; line-height:1.8em">             else if (x <= 15 && x >= -15 && y < 0)
  70.                 direct.Text = "上" + x + ",serif; font-size:12px; line-height:1.8em">             else if (x <= 15 && x >= -15 && y > 0)
  71.                 direct.Text = "下" + x + ",serif; font-size:12px; line-height:1.8em">             else if (y <= 15 && y >= -15 && x < 0)
  72.                 direct.Text = "左" + x + ",serif; font-size:12px; line-height:1.8em">             else if (y <= 15 && y >= -15 && x > 0)
  73.                 direct.Text = "右" + x + ",serif; font-size:12px; line-height:1.8em">                 direct.Text = "";
  74.         }
复制代码
这样,就可以多点触控了。而且,无论怎样操作,我的方向盘和那么的所谓的A键,就能发出“命令”了。 结果实在是不好截图。毕竟在真机上调试的。把工程放上来吧。http://115.com/file/c2p3tv39#MultiTouchDemo.zip 原文地址:http://blog.csdn.net/fengyun1989/article/details/7361429

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐