且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在UWP中使用滑动手势

更新时间:2022-01-02 22:59:37

我正在对可在控件上应用的TextBlock应用滑动.

I am applying swipe on a TextBlock you can apply on your control.

XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Name="SwipeableTextBlock" 
               HorizontalAlignment="Stretch"
               VerticalAlignment="Stretch"
               TextAlignment="Center" Text="No Swipe"
               FontSize="65" FontWeight="Light"
               ManipulationMode="TranslateX,TranslateInertia,System" 
               ManipulationDelta="SwipeableTextBlock_ManipulationDelta"
               ManipulationCompleted="SwipeableTextBlock_ManipulationCompleted"/>
</Grid>

C#

private bool _isSwiped;

private void SwipeableTextBlock_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    if (e.IsInertial && !_isSwiped)
    {
        var swipedDistance = e.Cumulative.Translation.X;

        if (Math.Abs(swipedDistance) <= 2) return;

        if (swipedDistance > 0)
        {
            SwipeableTextBlock.Text = "Right Swiped";
        }
        else
        {
            SwipeableTextBlock.Text = "Left Swiped";
        }
        _isSwiped = true;
    }            
}

    private void SwipeableTextBlock_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
    {
        _isSwiped = false;
    }     

输出 (在PC和Mobile上均可使用) 也归功于 @JustinXL 答案,这是示例

Output (Works On PC and Mobile Both) also credits to @JustinXL answer and this is sample repository