#741 – 使用触摸事件移动控件(Using Touch Manipulation Events to Translate an Element)
2017-06-28 14:58
489 查看
原文地址:https://wpf.2000things.com/2013/01/24/741-using-touch-manipulation-events-to-translate-an-element/
你可以通过使用触摸操作事件在屏幕上移动控件。让控件随着手指的移动在屏幕上移动。
首先,需要设置需要接收触摸操作事件的控件的IsManipulationEnabled 属性为true ,来开启控件接收触摸操作事件。同时还要为ManipulationStarting 和ManipulationDelta 事件注册事件处理函数。
上面的代码中,允许用户通过手指移动图像。我们绑定图片控件的
你可以通过使用触摸操作事件在屏幕上移动控件。让控件随着手指的移动在屏幕上移动。
首先,需要设置需要接收触摸操作事件的控件的IsManipulationEnabled 属性为true ,来开启控件接收触摸操作事件。同时还要为ManipulationStarting 和ManipulationDelta 事件注册事件处理函数。
<Canvas Name="canvMain" Background="Transparent"> <Image Source="JamesII.jpg" Width="100" IsManipulationEnabled="True" RenderTransform="{Binding ImageTransform}" ManipulationStarting="Image_ManipulationStarting" ManipulationDelta="Image_ManipulationDelta"/> </Canvas>
上面的代码中,允许用户通过手指移动图像。我们绑定图片控件的
RenderTransform属性为一个MatrixTransform 类型的实例(后面在代码中定义)。该实例包含一个控制图像“缩放”、“旋转”和“位移”的矩阵。在我们这个例子中,我们只在ManipulationDelta事件中修改矩阵控制位移部分的矢量。
public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); this.DataContext = this; ImageTransform = new MatrixTransform(); // XAML中绑定的MatrixTransform 实例 } private MatrixTransform imageTransform; public MatrixTransform ImageTransform { get { return imageTransform; } set { if (value != imageTransform) { imageTransform = value; RaisePropertyChanged("ImageTransform"); } } } private void Image_ManipulationStarting(object sender, ManipulationStartingEventArgs e) { // Ask for manipulations to be reported relative to the canvas e.ManipulationContainer = canvMain; } private void Image_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) { ManipulationDelta md = e.DeltaManipulation; // 获取当前操作的最新更改 Vector trans = md.Translation; //获取操作在屏幕上水平和垂直移动信息 // Update matrix to reflect translation Matrix m = imageTransform.Matrix; m.Translate(trans.X, trans.Y); // 设置图像在屏幕上的水平和垂直移动距离 imageTransform.Matrix = m; RaisePropertyChanged("ImageTransform"); e.Handled = true; } public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string prop) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } }
相关文章推荐
- #747 –在WPF程序的触摸操作中使用惯性移动 (Implementing Inertia during Touch Manipulation)
- #713 – 在拖拽操作中使用控件预览图作为鼠标(Setting the Cursor to an Image of an UIElement While Dragging)
- Responding to Touch Events 响应触摸事件
- #686 – 使用动画是控件具有闪烁效果(Using Animation to Make an Image Pulse)
- cocos2d-x项目101次相遇:使用触摸事件移动 精灵
- javascript触摸事件touch使用
- Android inject input events 注入Touch 点(x, y) 触摸输入事件
- cocos2d-x 2.2.2 中cocostudio控件TouchGroup触摸事件
- (OnTouchListener) android中的触摸事件的使用(绘图)
- cocos2d-x项目101次相遇:使用触摸事件移动 精灵
- 在Quick Cocos2d-x中给CocosStudio中导出的UI(Widget)控件增加触摸(touch)事件
- cocos2d-x项目101次相遇:使用触摸事件移动 精灵
- How to use toolbar control on dialog using CToolBar class in VC++ - VC++ 中用CToolBar 类如何使用ToolBar控件
- 控件:TouchProject(绘图) --- 触摸事件
- 与众不同 windows phone (24) - Input(输入)之软键盘类型, XNA 方式启动软键盘, UIElement 的 Touch 相关事件, 触摸涂鸦
- android 在ViewGroup中处理触摸事件 [Managing Touch Events in a ViewGroup]
- starling教程-触摸事件(Touch Events)(四)
- 使用伪寄存器调试MSVC++程序的介绍--An introduction to debugging in MSVC++ using Pseudoregisters
- Using an Intent to make a photo--Android使用Intent拍照
- 使用Android绘图技术绘制一个椭圆形,然后通过触摸事件让该椭圆形跟着手指移动