【问题标题】:Rotate soft on screen buttons on windows phone UWP在 Windows Phone UWP 上的屏幕按钮上软旋转
【发布时间】:2017-04-22 12:53:22
【问题描述】:

有谁知道如何在 C#、windows phone 10 中的方向更改时在屏幕按钮上进行软旋转?

我希望将显示方向保持为纵向或横向以避免在显示方向更改时出现动画。我可以在 DeviceOrientation.OrientationChanged 事件上旋转所有内容按钮,但我不知道是否有办法访问和旋转屏幕按钮。

我找到了标记为已回答的多个答案,如下所示: UWP C# Disable Orientation Change Animation 但他们都在谈论如何检测方向是否改变。我找不到任何关于如何在屏幕按钮上进行软旋转的示例。

效果应该和windows phone 原生相机一样。

【问题讨论】:

    标签: c# button uwp screen-orientation device-orientation


    【解决方案1】:

    您可以使用方向传感器获取设备的方向,然后设置 Button.Projection

    的角度

    参考链接:https://msdn.microsoft.com/en-us/windows/uwp/devices-sensors/use-the-orientation-sensor

    public sealed partial class MainPage : Page
    {
        private SimpleOrientationSensor simpleorientation;
        private double? Current = 0;
    
        public MainPage()
        {
            this.InitializeComponent();
            simpleorientation = SimpleOrientationSensor.GetDefault();
            if (simpleorientation != null)
            {
                simpleorientation.OrientationChanged += new TypedEventHandler<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged);
            }
        }
    
        private async void OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                SimpleOrientation orientation = args.Orientation;
                switch (orientation)
                {
                    case SimpleOrientation.NotRotated:
                        Rotate(0);
                        break;
    
                    case SimpleOrientation.Rotated90DegreesCounterclockwise:
                        Rotate(90);
                        break;
    
                    case SimpleOrientation.Rotated180DegreesCounterclockwise:
                        Rotate(180);
                        break;
    
                    case SimpleOrientation.Rotated270DegreesCounterclockwise:
                        Rotate(270);
                        break;
    
                    case SimpleOrientation.Faceup:
                        break;
    
                    case SimpleOrientation.Facedown:
                        break;
    
                    default:
    
                        break;
                }
            });
        }
    
        private void Rotate(double value)
        {
            MyAnimation.From = Current;
            MyAnimation.To = (360 - value);
            myStoryBoard.Begin();
            Current = MyAnimation.To;
        }
    }
    

    XAML 代码:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button
            x:Name="TestButton"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Content="button">
            <Button.Projection>
                <PlaneProjection x:Name="Projection" RotationZ="0" />
            </Button.Projection>
        </Button>
        <Grid.Resources>
            <Storyboard x:Name="myStoryBoard">
                <DoubleAnimation
                    x:Name="MyAnimation"
                    Storyboard.TargetName="Projection"
                    Storyboard.TargetProperty="RotationZ"
                    Duration="0:0:1" />
            </Storyboard>
        </Grid.Resources>
    </Grid>
    

    【讨论】:

    • 感谢您的回复。这不只是为我在网格中放在屏幕上的按钮设置动画吗?我对如何在屏幕按钮上旋转硬件感兴趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2011-05-13
    • 2011-01-21
    • 2010-09-23
    • 1970-01-01
    相关资源
    最近更新 更多