2017-02-24 213 views
0

我想通過手勢來移動/縮放/旋轉用戶控件,並且我希望旋轉和縮放的中心點位於手勢的中心(例如,當使用兩個手指旋轉,手指之間的點應該是旋轉的中心)。圍繞手勢點旋轉/縮放

當我不試圖設置旋轉/縮放的中心點或設置一個靜態點時,一切都按預期工作。將CompositeTransform.CenterX/Y設置爲ManipulationDeltaRoutedEventArgs.Position的值時,usercontrol將隨着每個手勢更加錯誤的中心點旋轉,並偶爾會加速。

我使用的是CompositeTransform作爲渲染變換我的用戶控制的,我已經迷上了在ManipulationDelta事件,像這樣:

private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) 
    { 
     //this.transform is my composite transform 
     //which is set to be the render transform of the user control 
     this.transform.CenterX = e.Position.X; 
     this.transform.CenterY = e.Position.Y; 
     this.transform.ScaleX *= e.Delta.Scale; 
     this.transform.ScaleY *= e.Delta.Scale; 
     this.transform.Rotation += e.Delta.Rotation; 
     this.transform.TranslateX += e.Delta.Translation.X; 
     this.transform.TranslateY += e.Delta.Translation.Y; 
    } 

看來,e.Position不給我我想要什麼,不幸的是文檔非常簡短,只能說明Gets the point from which the manipulation originated.從我的調試打印中看來,CompositeTransform.CenterX/Y和ManipulationDeltaRoutedEventArgs.Position都位於用戶控件的座標系中。

回答

1

問題原來是CompositeTransform只能處理一個中心點。因此,當中心點發生變化時,它也會對所有以前的變換進行追溯改變。解決方案是使用TransformGroup並使用自己的中心點創建單獨的轉換:

private void UserControl_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e) 
    { 
     var localCoords = e.Position; 
     var relativeTransform = this.TransformToVisual(this.Container); 
     Point parentContainerCoords = relativeTransform.TransformPoint(localCoords); 
     var center = parentContainerCoords; 

     RotateTransform rotation = new RotateTransform(); 
     rotation.CenterX = center.X; 
     rotation.CenterY = center.Y; 
     rotation.Angle = e.Delta.Rotation; 
     this.transformGroup.Children.Add(rotation); 

     ScaleTransform scaling = new ScaleTransform(); 
     scaling.CenterX = center.X; 
     scaling.CenterY = center.Y; 
     scaling.ScaleX = e.Delta.Scale; 
     scaling.ScaleY = e.Delta.Scale; 
     this.transformGroup.Children.Add(scaling); 

     TranslateTransform translation = new TranslateTransform(); 
     translation.X = e.Delta.Translation.X; 
     translation.Y = e.Delta.Translation.Y; 
     this.transformGroup.Children.Add(translation); 
    }