【问题标题】:Rotate Ui object on View by mouse UWP通过鼠标 UWP 在视图上旋转 Ui 对象
【发布时间】:2017-02-19 20:53:27
【问题描述】:

我需要使用鼠标旋转Image。 我不知道该怎么做。现在我有了按下按钮进行旋转的代码

 private void Rotate_Click(object sender, RoutedEventArgs e)
    {
        var button = sender as Button;
        var parent = (FrameworkElement) VisualTreeHelper.GetParent(button);
        var ct = (CompositeTransform) parent.RenderTransform;
        ct.Rotation += 5;
    }

但是我怎样才能将其更改为我的要求?我需要拖动那个按钮并旋转

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    这是一个粗略的示例,显示视图上的图像在拖动时会围绕其中心旋转。

    在新项目中添加此 XAML

    <Image x:Name="TheImage"
            Source="Assets/StoreLogo.png"
            Width="200"
            Stretch="Uniform"
            RenderTransformOrigin="0.5,0.5"
            PointerPressed="OnPointerPressed"
            PointerMoved="OnPointerMoved"
            PointerReleased="OnPointerReleased">
        <Image.RenderTransform>
            <RotateTransform x:Name="ImageRotation" />
        </Image.RenderTransform>
    </Image>
    

    这是随附的代码隐藏

    private bool pointerCaptured = false;
    private Point lastPosition;
    
    private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
    {
        pointerCaptured = true;
        this.lastPosition = e.GetCurrentPoint(TheImage).Position;
    }
    
    private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
    {
        if (pointerCaptured)
        {
            Point currentLocation = e.GetCurrentPoint(this.TheImage).Position;
    
            double radians = Math.Atan((currentLocation.Y - lastPosition.Y) /
                                        (currentLocation.X - lastPosition.X));
            var angle = radians * 180 / Math.PI;
    
            // Apply a 180 degree shift when X is negative so can rotate all of the way around
            if (currentLocation.X - lastPosition.X < 0)
            {
                angle += 180;
            }
    
            lastPosition = currentLocation;
    
            this.ImageRotation.Angle = angle;
        }
    }
    
    private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
    {
        pointerCaptured = false;
    }
    

    一些旋转数学的帽子提示https://stackoverflow.com/a/963099/1755

    【讨论】:

    • 嗯,我在执行上有一些麻烦。我无法设置&lt;RotateTransform/&gt;,因为我有CompositeTransform,并且我需要拖动一个按钮女巫放置在图像附近的相邻单元格中。而我的 Grid 地方是 DataTemplate joxi.ru/Dr83OBecLD1JA6 你的方法可以在我的结构中实现吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2013-03-17
    • 1970-01-01
    • 2017-01-05
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多