WPF Multi-Touch 开发:惯性效果(Inertia)


从上一篇实例可以发现在图片移动过程中如果将手指移开屏幕则图片会立刻停止,根据这种情况WPF 提供另外一种惯性效果(Inertia)。通过它可以使UI 单元移动的更加符合物理特性、更为实际和流畅。

  在前面代码的基础上还需要为<Canvas> 增加ManipulationInertiaStarting 事件。

<Canvas x:Name="touchPad" Background="Gray"         
    ManipulationStarting="image_ManipulationStarting"  
    ManipulationDelta="image_ManipulationDelta" 
    ManipulationCompleted="image_ManipulationCompleted" 
    ManipulationInertiaStarting="image_ManipulationInertiaStarting"> 

  image_ManipulationInertiaStarting 事件内容如下代码,分别对TranslationBehavior、ExpansionBehavior、RotationBehavior 进行设置,使其具备惯性特征。

private void image_ManipulationInertiaStarting(object sender, 
  ManipulationInertiaStartingEventArgs e) 
{ 
  e.TranslationBehavior = new InertiaTranslationBehavior(); 
  e.TranslationBehavior.InitialVelocity = e.InitialVelocities.LinearVelocity; 
  e.TranslationBehavior.DesiredDeceleration = 10.0 * 96.0 / (1000.0 * 1000.0); 
   
  e.ExpansionBehavior = new InertiaExpansionBehavior(); 
  e.ExpansionBehavior.InitialVelocity = e.InitialVelocities.ExpansionVelocity; 
  e.ExpansionBehavior.DesiredDeceleration = 0.1 * 96 / 1000.0 * 1000.0; 
   
  e.RotationBehavior = new InertiaRotationBehavior(); 
  e.RotationBehavior.InitialVelocity = e.InitialVelocities.AngularVelocity; 
  e.RotationBehavior.DesiredDeceleration = 720 / (1000.0 * 1000.0); 
}

  程序演示

  通过下面视频可以看到在进行Manipulation 时,图片都已具备惯性效果。

  视频地址:http://static.youku.com/v1.0.0122/v/swf/qplayer.swf?VideoIDS=XMTk4MjUyNDAw&embedid=MTI1LjY5LjE0NC4zNgI0OTU2MzEwMAJ3d3cuY25ibG9ncy5jb20CL2duaWVsZWUvYXJjaGl2ZS8yMDEwLzA4LzE5L211bHRpLXRvdWNoLWluZXJ0aWEuaHRtbA==&showAd=0

  源码下载地址:http://public.blu.livefilestore.com/y1pA8pt32bGc_1znfRppqLoDSS7hKRAougUyNOv2NOkG_N-EA4vzYytWkokn5XC7E3abor2YYWWR1CbUTOgq3mpgg/WpfInertia.zip?download&psid=1


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3