Dim overlay by swiping on Windows Phone

那年仲夏 提交于 2019-12-14 02:22:00

问题


I have a rectangle as an overlay and I want to modify its opacity by swiping up and down, like in the app Free Talking Alarm Clock. I figured out, that I have to deal with ManipulationStarted and ManipulationDelta events, but not sure which value to use. At this point I'm able to change the opacity by swiping, but changes only when the swipe is completed, and I want the opacity to change while I'm swiping. How do I do that?

Right now, I have this:

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");
    }
}

回答1:


Basically, the distance of your finger movement should be directly proportional to the Rectangle's Opacity change.

You can monitor the most recent Y change which is e.Delta.Translation.Y and divide it by the Rectangle's ActualHeight in order to get a more accurate Opacity change.

Something like this will do.

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;
}



回答2:


the code above works, just have to change the opacity value from 0.1 to 0.01



来源:https://stackoverflow.com/questions/28008698/dim-overlay-by-swiping-on-windows-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!