WPF中的多点触摸事件

原文:WPF中的多点触摸事件

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手势的移动,旋转和缩放,也加入了各种触摸事件来处理多个点的触摸,通过这些事件可以获取到多点触摸的坐标,从而实现各种多点逻辑。是否觉得很强大?

原文地址:https://www.cnblogs.com/lonelyxmas/p/9589379.html

时间: 2024-08-04 05:56:17

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

Cocos2d-x 3.0中实现多点触摸

Cocos2d-x 3.0中实现多点触摸 尊重原创:http://cn.cocos2d-x.org/tutorial/show?id=2713 在上一篇<Cocos2d-x 3.0 中使用单点触摸>中介绍了在Cocos2d-x 3.0中实现单点触摸,但是有些游戏还会用到多点触摸,其中最典型的游戏是节奏大师,在节奏大师中会不断产生运动的音符,玩家需要不停地点击音符以获得高分,而且玩家可以多个手指头一起点,多个手指头一起点就是使用多点触摸实现的. 下面通过一个小的例子介绍如何在Cocos2d-x

正确处理WPF中Slider值改变事件的方式

最近在用WPF数据绑定重写一下播放器项目时遇到的关于Slider的问题,在窗体透明度调节和播放进度调节上用了Slider控件.调节窗体透明度我是 这么想的:将窗体的Opacity属性的值与Slider的值绑定不就可以了,Opacity="{Binding ElementName=Slider,Path=Value,Mode=OneWay}",这样根本不用处理Slider的值改变事件 (ValueChanged).不过我要做保存设置的功能,因此肯定要记录Slider的值咯,于是处理一下V

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

原文:在VS2005中设置WPF中自定义按钮的事件 上篇讲了如何在Blend中绘制圆角矩形(http://blog.csdn.net/johnsuna/archive/2007/08/13/1740781.aspx),本篇继续下一步骤,如何自定义按钮的事件. (1)首先,在VS2005中打开上篇所建的项目(File - Open Project),找到LinearGradientButton.csproj(这是我这里的项目名称),打开之后,双击LinearGradientDemo.xaml.cs

Android中的多点触摸

代码下载地址 代码一:自定义支持多点触摸的TextView http://download.csdn.net/detail/zhiyuan0932/9513852 什么是多点触摸 允许计算机用户同时通过多个手指来控制图形界面的一种技术 多点触摸的应用场景 对图片.文字.网页进行放大或者缩小 多手指手势操作自定义控件和布局 触摸事件的重要方法 event.getActionMasked(): 获取事件类型 在只使用单手指操作的时候,这个方法我们一般使用的是event.getAction(),来获取

iOS中的四中触摸事件的详解 - 平移- 捏合 - 滑动(TouchesBegan,touchesMoved,touchesEnded,touchesCancelled)

RootViewController #import "RootViewController.h" #import "TouchView.h" #import "PanView.h" #import "PinchView.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad { [super

zepto中的多点触摸

在用zepto中做屏幕的触摸来滑动图片的过程中,获取touchstart中的数据,是用e.touches[0],当时我就奇怪为什么是放在数组里面,而且打印出来的全部都是长度为1的touches数组. 后面,我想做两只手指来滑动图片的时候,才觉得一条触摸路径对应一个touches数组中的数据.但是在手机端无法console.log()测试.于是我定义了一个变量touchestime,用来记录touches的次数.touchstart的时候就++,touchend的时候就--,然后等于2的时候就al

cocos2d-x之多点触摸事件

1 //全部所有的触摸点一起触发 2 auto listener = EventListenerTouchAllAtOnce::create(); 3 4 listener->onTouchesBegan = [](std::vector<Touch*> ts, Event *e){ 5 log("onTouchesBegan"); 6 }; 7 8 //获得触摸点的数量 9 listener->onTouchesMoved = [](std::vector&l

WPF中TextBox的PreviewMouseLeftButtonUp事件

当使用TextBox的PreviewMouseLeftButtonUp事件时(例如,鼠标点击进入TextBox时,清除当前的输入内容),会很意外地发现,这时候不论怎么点击都无法点击到其他控件,焦点一直被文本框占用着. 解决办法及测试用例如下: 界面 1 <Window x:Class="learnwpf.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

WPF 多点触摸开发[2]:WPF触摸的几个手势的执行顺序

原文:WPF 多点触摸开发[2]:WPF触摸的几个手势的执行顺序 前面我讲了在win7下使用模拟器,进行调试模拟多点触摸,其实际开发中这样也比较麻烦.. 要拿几个鼠标. 所以更多的人会 买个触摸套 套在显示屏上. 这样可支持 2点触摸. 又没有正真触摸屏的昂贵,价格也相对优惠很多. 接下来进入正题 . WPF其实本身对触摸事件就支持. 其中有些高及触屏的操作的手势路由事件. 大大增强了用户体验性. 使得我们能做出类似iphone手机的的触控效果.并使之这些柔和的效果 移植到桌面触摸应用上. 首先