手指之舞(三):多点触摸实现并且模拟GestureListener *** 作

手指之舞(三):多点触摸实现并且模拟GestureListener *** 作,第1张

概述  经过多点探索,实在没有办法,只有重回到silverlight低级触摸事件了。但是对GestureListener还有点依依不舍,毕竟那个 *** 作起来方便。现在没办法,只有自己写,那就模拟一下吧。 现在重写上一篇那个实况方向控制盘。 先把界面做好: <Ellipse x:Name="innerRound" Width="100" Height="100" VerticalAlignment="Top

  经过多点探索,实在没有办法,只有重回到silverlight低级触摸事件了。但是对GestureListener还有点依依不舍,毕竟那个 *** 作起来方便。现在没办法,只有自己写,那就模拟一下吧。

现在重写上一篇那个实况方向控制盘。

先把界面做好:

<Ellipse x:name="innerRound" WIDth="100" Height="100" VerticalAlignment="top"                     HorizontalAlignment="left" margin="100,350,0" Fill="Red"                     stroke="Blue" strokeThickness="5" >                <Ellipse.Rendertransform>                    <Translatetransform x:name="innerTran"/>                </Ellipse.Rendertransform>            </Ellipse>             <TextBlock x:name="textblock1" WIDth="150" Height="80" HorizontalAlignment="left"                       VerticalAlignment="top" Text="click Me" FontSize="40" margin="300,0"/>            <TextBlock x:name="direct" Text="direct" WIDth="200" Height="100"                        HorizontalAlignment="left"                       VerticalAlignment="top"                       FontSize="40" margin="250,200,0"/>            <TextBlock x:name="click" Text="click" WIDth="150" Height="100"                        HorizontalAlignment="left"                       VerticalAlignment="top"                       FontSize="40" margin="300,300,0"/>


界面就不截图了。和上一篇的界面基本一样,就是颜色不太一样。放置了两个Textblock来作为多点触控测试用。


然后在PageLoaded注册事件:

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掉那个手指,只有就能让新的手指进来 *** 作方向盘了。


添加两个成员变量:

        Dictionary<int,Point> EtouchDict = new Dictionary<int,Point>();          Translatetransform tr = new Translatetransform();



具体代码如下:

voID touch_FrameReported(object sender,touchFrameEventArgs e)        {            touchPoint primarytouchPoint = e.GetPrimarytouchPoint(null);            // Inhibit mouse promotion            if (primarytouchPoint != null && primarytouchPoint.Action == touchAction.Down)                e.SuspendMousePromotionUntiltouchUp();            touchPointCollection touchPoints = e.GettouchPoints(null);            foreach (var item in touchPoints)            {                if (item.Action == touchAction.Up)                {                    if (EtouchDict.ContainsKey(item.touchDevice.ID))                    {                        EtouchDict.Remove(item.touchDevice.ID);                        this.innerTran.X = 0;                        this.innerTran.Y = 0;                        showDirect();                    }                }                if (item.touchDevice.DirectlyOver == textblock1) {                    //DeBUG.Writeline("textblock1 is touch!  "  + item.Action.ToString());                    click.Text = "A";                    click.Foreground = new SolIDcolorBrush(                            color.FromArgb(255,(byte)rand.Next(256),(byte)rand.Next(256)));                }                else if (item.touchDevice.DirectlyOver == innerRound)                {                    //DeBUG.Writeline("rectangle is touch!  " + item.Action.ToString());                    EllipsetouchHandle(item);                }            }                    }        voID EllipsetouchHandle(touchPoint tp)        {            if (EtouchDict.Count() > 0 && !EtouchDict.ContainsKey(tp.touchDevice.ID) )            {                return;            }            if (EtouchDict.Count() <= 0)	        {                EtouchDict.Add(tp.touchDevice.ID,tp.position);                //DeBUG.Writeline("Ellipse is touch!  x:" + tp.position.X + " Y:" + tp.position.Y);	        }            else            {                Point LastPoint = EtouchDict.FirstOrDefault().Value;                double horX = tp.position.X - LastPoint.X;                double verY = tp.position.Y - LastPoint.Y;                double x = this.innerTran.X + horX;                double y = this.innerTran.Y + verY;                if (x <= -50)                    this.innerTran.X = -50;                else if (x >= 50)                    this.innerTran.X = 50;                else                    this.innerTran.X = x;                if (y <= -50)                    this.innerTran.Y = -50;                else if (y >= 50)                    this.innerTran.Y = 50;                else                    this.innerTran.Y = y;                showDirect();                //DeBUG.Writeline("Ellipse is touch! HorizontalVeLocity(X): " + horX.ToString() + " VerticalVeLocity(Y):" + verY.ToString());                EtouchDict.Remove(tp.touchDevice.ID);                EtouchDict.Add(tp.touchDevice.ID,tp.position);            }        }        voID showDirect()        {            double x = innerTran.X;            double y = innerTran.Y;            if (x <= -15 && x >= -50 && y <= -15 && y >= -50 )            {                direct.Text = "左上" + x + "," + y;             }            else if (x >= 15 && x <= 50 && y <= -15 && y >= -50)            {                direct.Text = "右上" + x + "," + y;            }            else if (x <= -15 && x >= -50 && y >= 15 && y <= 50)            {                direct.Text = "左下" + x + "," + y;            }            else if (x >= 15 && x <= 50 && y >= 15 && y <= 50)            {                direct.Text = "右下" + x + "," + y;            }            else if (x <= 15 && x >= -15 && y < 0)            {                direct.Text = "上" + x + "," + y;            }            else if (x <= 15 && x >= -15 && y > 0)            {                direct.Text = "下" + x + "," + y;            }            else if (y <= 15 && y >= -15 && x < 0)            {                direct.Text = "左" + x + "," + y;            }            else if (y <= 15 && y >= -15 && x > 0)            {                direct.Text = "右" + x + "," + y;            }            else            {                direct.Text = "";            }        }

这样,就可以多点触控了。而且,无论怎样 *** 作,我的方向盘和那么的所谓的A键,就能发出“命令”了。

结果实在是不好截图。毕竟在真机上调试的。把工程放上来吧。http://115.com/file/c2p3tv39#MultiTouchDemo.zip

总结

以上是内存溢出为你收集整理的手指之舞(三):多点触摸实现并且模拟GestureListener *** 作全部内容,希望文章能够帮你解决手指之舞(三):多点触摸实现并且模拟GestureListener *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1069323.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存