原文 WPF4多点触摸事件

UIElement在WPF4下添加了很多支持多点触摸的事件,通过它们可以在硬件支持的情况下处理多点触摸,以下通过代码来说明通过处理这些事件,我们可以做些什么:

一.触摸相关的多种事件,跟鼠标事件是对应的,通过这些事件可以获取到多个触摸的鼠标点,并进行相应的处理

  • public static readonly RoutedEvent TouchDownEvent;
  • public static readonly RoutedEvent TouchEnterEvent;
  • public static readonly RoutedEvent TouchLeaveEvent;
  • public static readonly RoutedEvent TouchMoveEvent;
  • public static readonly RoutedEvent TouchUpEvent;

以上每个事件都包含一个TouchEventArgs参数,通过该参数可以获取到一个TouchDevice信息,对应于每一次触摸,还可以通过 GetTouchPoint得到一个TouchPoint,TouchPoint包含当前触摸的动作,触摸的位置等信息,通过获取到的 TouchDevice,我们可以处理每一次触摸(通过判断TouchDevice的ID号来分辨不同的触摸),并通过TouchPoint获取触摸的坐 标点,从而实现一些多点的逻辑,例如多点的书写(通过获取的TouchPoint来生成PathFigure,形成PathGeometry,最终填充成 Path来绘制)

二.Manipulation事件,通过这些事件可以实现UIElement的一些多点手势(移动,旋转,缩放)

  • public static readonly RoutedEvent ManipulationCompletedEven;
  • public static readonly RoutedEvent ManipulationDeltaEvent;
  • public static readonly RoutedEvent ManipulationInertiaStartingEvent;
  • public static readonly RoutedEvent ManipulationStartedEvent;

1.要处理Manipulation事件,首先必须设置UIElement的IsManipulationEnabled为true

2.ManipulationInertiaStartingEvent事件包含一个ManipulationStartingEventArgs参数,通过该参数可以设置:

UIElement的ManipulationContainer —— 设置该UIElement的容器

Mode —— 处理的事件类型,包含以下枚举

None:不处理

TranslateX:处理水平移动

TranslateY:处理垂直移动

Translate:处理移动

Rotate:处理旋转

Scale:处理缩放

All:处理所有事件

3.要实现控件的移动,缩放,旋转,可以在控件的ManipulationDeltaEvent事件中使用以下代码:

private void image_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            var element = e.Source as FrameworkElement;
            if (element != null)
            {
                try
                {
                    ManipulationDelta deltaManipulation = e.DeltaManipulation;
                    Matrix matrix = element.RenderTransform.Value;
                    Point center = new Point(element.ActualWidth / 2, element.ActualHeight / 2); 
                    center = matrix.Transform(center);  //设置中心点
                    //处理缩放
                    matrix.ScaleAt(deltaManipulation.Scale.X, deltaManipulation.Scale.Y, center.X, center.Y);
                    // 处理旋转
                    matrix.RotateAt(e.DeltaManipulation.Rotation, center.X, center.Y);
                    //处理移动

matrix.Translate(e.DeltaManipulation.Translation.X, e.DeltaManipulation.Translation.Y);

element.RenderTransform = new MatrixTransform(matrix);

e.Handled = true;
                }
                catch (Exception ei)
                {
                    MessageBox.Show(ei.ToString());
                }
            }

}

4.此外可以在ManipulationInertiaStarting事件中设置惯性效果

private void image_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
        {
            // 移动惯性
            e.TranslationBehavior = new InertiaTranslationBehavior()
            {
                InitialVelocity = e.InitialVelocities.LinearVelocity,
                DesiredDeceleration = 1 / (1000.0 * 1000.0)   // 单位:一个WPF单位 / ms
            };

// 缩放惯性
            e.ExpansionBehavior = new InertiaExpansionBehavior()
            {
                InitialVelocity = e.InitialVelocities.ExpansionVelocity,
                DesiredDeceleration = 1 / 1000.0 * 1000.0   // 单位:一个WPF单位 / ms
            };

// 旋转惯性
            e.RotationBehavior = new InertiaRotationBehavior()
            {
                InitialVelocity = e.InitialVelocities.AngularVelocity,
                DesiredDeceleration = 720 / (1000.0 * 1000.0)  //单位:一个角度 / ms
            };
            e.Handled = true;
        }

5.在设置了惯性事件后,如果不处理判断控件容器的边界,那很容易一个移动就会把控件移到屏幕外部,因此此时可以在ManipulationDeltaEvent事件中加入以下代码:

if (e.IsInertial)
   {
           Rect containingRect = new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);

Rect shapeBounds = element.RenderTransform.TransformBounds(new Rect(element.RenderSize));
           if (e.IsInertial && !containingRect.Contains(shapeBounds))
           {
                 e.ReportBoundaryFeedback(e.DeltaManipulation);    
                 e.Complete();
            }
    }

三.总结

WPF4直接加入了Manipulation事件来支持对UIElement手势的移动,旋转和缩放,也加入了各种触摸事件来处理多个点的触摸,通过这些事件可以获取到多点触摸的坐标,从而实现各种多点逻辑

WPF4多点触摸事件的更多相关文章

  1. WPF中的多点触摸事件

    UIElement在WPF4下添加了很多支持多点触摸的事件,通过它们可以在硬件支持的情况下处理多点触摸,以下通过代码来说明通过处理这些事件,我们可以做些什么: 一.触摸相关的多种事件,跟鼠标事件是对应 ...

  2. 移动web开发,12个触摸及多点触摸事件常用Js插件

    如今移动互联网已经占据了主流地位,越来越多的开发者开始从桌面转向移动平台.与桌面开发不同的是,在移动领域中,不同的操作系统.大量不同屏幕尺寸的移动设备.触摸手势操作等,这都给开发者带来了一定的难度和挑 ...

  3. touch多点触摸事件

    touch--单点 targetTouches. changeTouches 多点: targetTouches--当前物体上的手指数 *不同物体上的手指不会互相干扰 不需要做多点触摸的时候---平均 ...

  4. 移动端-js触摸事件

    开发者工具 在移动开发中,一种较为容易的做法是,先在桌面上开始原型设计,然后再在打算要支持的设备上处理移动特有的部分.多点触摸正是难以在PC上进行测试的那些功能之一,因为大部分的PC都没有触摸输入. ...

  5. javascript触摸事件touch使用

    详细内容请点击 Apple在iOS 2.0中引入了触摸事件API,Android正迎头赶上这一事实标准,缩小差距.最近一个W3C工作组正合力制定这一触摸事件规范.        在本文深入研究iOS和 ...

  6. Cocos2d-x实例:单点触摸事件

    addChild(boxC,30, kBoxC_Tag);                                                                        ...

  7. Cocos2d-x中触摸事件

    理解一个触摸事件可以从时间和空间两方面考虑. 1.触摸事件的时间方面 触摸事件的在时间方面,如下图所示,可以有不同的“按下”.“移动”和“抬起”等阶段,表示触摸是否刚刚开始.是否正在移动或处于静止状态 ...

  8. 【转】基于Qt, TUIO和TSLIB的嵌入式Linux下的多点触摸设计

    这个教程描述了在嵌入式linux下使用Qt如何设置一个支持多点触摸和单点触摸的输入系统.这里假定你已经有了对应的驱动程序,驱动可以从触摸屏的厂商那里获得或者使用一个linux 内核源码中已经存在的驱动 ...

  9. Swift - 触摸事件(点击,移动,抬起等)说明及用例

    在iOS开发中,UIGestureRecognizer可以方便的响应处理手势事件. 而如果要想更精细的处理,我们还需要借助touchesBegan,touchesMoved,touchesEnded等 ...

随机推荐

  1. How Many Answers Are Wrong(并查集)

    Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he ...

  2. [C++参考]私有成员变量的理解

    私有成员变量的概念,在脑海中的现象是,以private关键字声明,是类的实现部分,不对外公开,不能在对象外部访问对象的私有成员变量. 然而,在实现拷贝构造函数和赋值符函数时,在函数里利用对象直接访问了 ...

  3. What is Webhook ( Introduction to Webhook )

    A webhook in web development is a method of augmenting or altering the behavior of a web page, or we ...

  4. 转: requirejs压缩打包r.js使用示例 2 (~~很详细的教程)

    这一篇来认识下打包工具的paths参数,在入门一中 就介绍了require.config方法的paths参数.用来配置jquery模块的文件名(jQuery作为AMD模块时id为“jquery”, 但 ...

  5. 关于异或(Xor)的一点笔记

    因为博弈论里,尤其实在求sg函数时,经常会用到异或运算,所以我就把网上搜到的一些相关知识和自己的一些理解记下来. 如果出现差错,还请指出,谢谢! 异或:可以简称Xor,可以用数学符号⊕表示,计算机就一 ...

  6. java学习之xml

    xml的处理有两种方式dom和Sax 其中dom有3套api ,分别是dom和jdom和dom4j package com.gh.xml; import java.io.File; import ja ...

  7. 饭卡------HDOJ杭电2546(还是01背包!!!!!!)

    Problem Description 电子科大本部食堂的饭卡有一种非常诡异的设计,即在购买之前推断剩余金额. 假设购买一个商品之前,卡上的剩余金额大于或等于5元,就一定能够购买成功(即使购买后卡上剩 ...

  8. Android创建启动画面

    每一个Android应用启动之后都会出现一个Splash启动界面,显示产品的LOGO.公司的LOGO或者开发人员信息.假设应用程序启动时间比較长,那么启动界面就是一个非常好的东西,能够让用户耐心等待这 ...

  9. unix ls命令

    [语法]: ls  [-RadCxmlnogrtucpFbqisf1]   [文件夹或文件......] [说明]: ls 命令列出指定文件夹下的文件,缺省文件夹为当前文件夹 ./,缺省输出顺序为纵向 ...

  10. python字符串方法以及注释

    转自fishC论坛:http://bbs.fishc.com/forum.php?mod=viewthread&tid=38992&extra=page%3D1%26filter%3D ...