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 / , element.ActualHeight / );
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 = / (1000.0 * 1000.0) // 单位:一个WPF单位 / ms
}; // 缩放惯性
e.ExpansionBehavior = new InertiaExpansionBehavior()
{
InitialVelocity = e.InitialVelocities.ExpansionVelocity,
DesiredDeceleration = / 1000.0 * 1000.0 // 单位:一个WPF单位 / ms
}; // 旋转惯性
e.RotationBehavior = new InertiaRotationBehavior()
{
InitialVelocity = e.InitialVelocities.AngularVelocity,
DesiredDeceleration = / (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手势的移动,旋转和缩放,也加入了各种触摸事件来处理多个点的触摸,通过这些事件可以获取到多点触摸的坐标,从而实现各种多点逻辑。是否觉得很强大?

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

  1. MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件

    原文  MSDN 杂志:UI 前沿技术 - WPF 中的多点触控操作事件 UI 前沿技术 WPF 中的多点触控操作事件 Charles Petzold 下载代码示例 就在过去几年,多点触控还只是科幻电 ...

  2. WPF4多点触摸事件

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

  3. 内核中的多点触摸协议文档 Multi【转】

    转自:http://www.arm9home.net/read.php?tid=24754 前段时间改写了一个GT801的内核驱动,仔细阅读 MT Event 上报的时候,发现这个驱动是针对 Andr ...

  4. 在VS2005中设置WPF中自定义按钮的事件

    原文:在VS2005中设置WPF中自定义按钮的事件 上篇讲了如何在Blend中绘制圆角矩形(http://blog.csdn.net/johnsuna/archive/2007/08/13/17407 ...

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

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

  6. Flex中处理多点触摸和手势

    在Flex中多点触摸和手势都需要利用Multitiouch类来完成:1.supportsGestureEvents:判断是否支持手势2.supportsTouchEvents:判断是否支持多点触摸可以 ...

  7. iOS中响应者链条-触摸事件

    总体来说,分2个步骤: 一,从上到下寻找合适的控件来处理这个触摸事件.如下图,如果点击了黄色4,则UIApplication -> UIWindow -> 1白色 -> 2橙色 -& ...

  8. iOS中响应者链条-触摸事件,hitTest方法坐标转换

    总体来说,分2个步骤: 一,从上到下寻找合适的控件来处理这个触摸事件.如下图,如果点击了黄色4,则UIApplication -> UIWindow -> 1白色 -> 2橙色 -& ...

  9. touch多点触摸事件

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

随机推荐

  1. jQuery 插件写法2

    转载:http://www.xuanfengge.com/jquery-plug-in-written-summary-and-summary-of-writing-object-oriented-m ...

  2. Installing MySQL Connector/Python using pip v1.5

    The latest pip versions will fail on you when the packages it needs to install are not hosted on PyP ...

  3. Ubuntu 14.04 LTS 与Kylin

    现在是安装了Ubuntu 14.04 LTS 但是通过安装ubuntukylin 这个包居然实现了Kylin--原来这个自主研发还这么方便-呵呵 sudo apt-get install ubuntu ...

  4. [转]IIS7.5 添加expires头 提高性能

    本文转自:http://niutuku.com/tech/www/271454.shtml 对于页面中不经常变化的静态内容通过指定expires头,来进行浏览器端的缓存,减少每次访问时的请求. 原理: ...

  5. 去掉VC2010 编辑器里出现的红色波浪线

    在VC2010中浏览代码的时候就大片的红线看着不舒服 其实不关VS的事,原因在于visual assist.   在VAssistX菜单栏->Visual Assist X Options-&g ...

  6. [设计模式]<<设计模式之禅>>工厂方法模式

    1 女娲造人的故事 东汉<风俗通>记录了一则神话故事:“开天辟地,未有人民,女娲搏黄土做人”,讲述的内容就是大家非常熟悉的女娲造人的故事.开天辟地之初,大地上并没有生物,只有苍茫大地,纯粹 ...

  7. javascript+dom 做javascript图片库

    废话不多说 直接贴代码 <!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  8. HttpClient(4.3.5) - HTTP Header

    An HTTP message can contain a number of headers describing properties of the message such as the con ...

  9. MyBatis(3.2.3) - Handling enumeration types

    MyBatis supports persisting enum type properties out of the box. Assume that the STUDENTS table has ...

  10. Ehcache(2.9.x) - API Developer Guide, Write-Through and Write-Behind Caches

    About Write-Through and Write-Behind Caches Write-through caching is a caching pattern where writes ...