WPF4 多点触摸事件
来源:互联网 发布:真空收纳袋 知乎 编辑:程序博客网 时间:2024/05/17 09:01
WPF4多点触摸事件
刚找到的关于触摸屏编程的内容。
一.触摸相关的多种事件,跟鼠标事件是对应的,通过这些事件可以获取到多个触摸的鼠标点,并进行相应的处理
以上每个事件都包含一个TouchEventArgs参数,通过该参数可以获取到一个TouchDevice信息,对应于每一次触摸,还可以通过GetTouchPoint得到一个TouchPoint,TouchPoint包含当前触摸的动作,触摸的位置等信息,通过获取到的TouchDevice,我们可以处理每一次触摸(通过判断TouchDevice的ID号来分辨不同的触摸),并通过TouchPoint获取触摸的坐标点,从而实现一些多点的逻辑,例如多点的书写(通过获取的TouchPoint来生成PathFigure,形成PathGeometry,最终填充成Path来绘制)
二.Manipulation事件,通过这些事件可以实现UIElement的一些多点手势(移动,旋转,缩放)
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 多点触摸事件
- Android多点触摸事件捕获
- js多点触摸touch事件
- 检测手机多点触摸事件
- Cocos2d-3.x_触摸事件(单点触摸和多点触摸)
- android 单点和多点触摸事件
- android多点触摸事件的一些事
- android 向模拟发送多点触摸事件~
- 如何“排外”处理多点触摸事件
- android 模拟发送多点触摸事件
- 5.触摸touch,单点触摸,多点触摸,触摸优先和触摸事件的吞噬
- cocos2d-x——触摸事件(多点触摸)
- 多点触摸
- 多点触摸
- 多点触摸
- 多点触摸
- 多点触摸
- Cocos2d触摸:多点触摸
- PNG文件结构
- IO 篇一 :理论篇 (java in thingking 4th阅读笔记)
- 不用sizeof来求出int的大小(程序)
- ASP.NET身份验证机制membership入门——配置篇(1)
- 易想商城(YX-SHOP )V4.7.1 过期解决方法,所有源代码(已破解)
- WPF4 多点触摸事件
- IO篇二( IO典型的使用方法 thinking in java 4th 阅读笔记)
- 关于各种无法解析的外部符号问题的相应解决方案
- printf,sscanf的用法及sscanf和sprintf的返回值的含义!
- SDL解析——五步棋
- 回车执行
- 获得Sql Server2000的字段注释
- 什么是Base64编码?
- VC6.0 Realse工程转换为debug工程