通过在Windows Phone上滑动来淡化覆盖

时间:2015-01-18 10:31:13

标签: c# windows-phone-8.1 overlay swipe

我有一个矩形作为叠加层,我想通过向上和向下滑动来修改其不透明度,就像在应用程序Free Talking Alarm Clock中一样。我想,我必须处理ManipulationStartedManipulationDelta事件,但不确定要使用哪个值。此时我可以通过滑动更改不透明度,但仅在滑动完成时更改,并且我希望在我滑动时更改不透明度。我该怎么做?

现在,我有这个:

private Point initialpoint;
private double opacity;
private void rec_overlay_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {
        initialpoint = e.Position;
    }

private void rec_overlay_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    Point currentpoint = e.Position;

    if (initialpoint.Y - currentpoint.Y < 0)
    {
        opacity += 0.1;
        rec_overlay.Opacity = opacity;
        System.Diagnostics.Debug.WriteLine("Swipe down");
    }
    else if(initialpoint.Y - currentpoint.Y > 0)
    {
        opacity -= 0.1;
        rec_overlay.Opacity = opacity;
        System.Diagnostics.Debug.WriteLine("Swipe up");
    }
}

2 个答案:

答案 0 :(得分:1)

上面的代码可以工作,只需要将不透明度值从0.1改为0.01

答案 1 :(得分:0)

基本上,手指移动的距离应与Rectangle Opacity更改成正比。

您可以监控最近的Y更改 e.Delta.Translation.Y并将其除以Rectangle&#39; s ActualHeight以获得更准确Opacity更改。

这样的事情会发生。

private void rec_overlay_ManipulationDelta(object sender, Windows.UI.Xaml.Input.ManipulationDeltaRoutedEventArgs e)
{
    // keep the Opacity value within its boundary
    if (rec_overlay.Opacity > 1)
    {
        rec_overlay.Opacity = 1;
    }
    else if (rec_overlay.Opacity < 0)
    {
        rec_overlay.Opacity = 0;
    }

    // update the opacity whenever a movement happens
    rec_overlay.Opacity += -e.Delta.Translation.Y / this.rec_overlay.ActualHeight;
}