Xamarin.Forms。像电报一样滑动到上一页

时间:2019-01-14 15:38:37

标签: android xamarin.ios swipe

如何创建向后滑动?

在iOS中:

[assembly: ExportRenderer(typeof(HIPSTO.Controls.CustomContentPage), typeof(HIPSTO.iOS.Platform.Renderers.IOSPageRenderer))]     
namespace HIPSTO.iOS.Platform.Renderers 
{
    public class IOSPageRenderer : PageRenderer        
    {         
      public override void ViewWillAppear(bool animated)            
      {              
        base.ViewWillAppear(animated);

        ViewController.NavigationController.InteractivePopGestureRecognizer.Enabled = true;
        ViewController.NavigationController.InteractivePopGestureRecognizer.Delegate = new UIKit.UIGestureRecognizerDelegate();
       }
    }
}

但是它仅从边缘起作用。从任何地方都有必要。 android没有想法

如下图所示:

enter image description here

1 个答案:

答案 0 :(得分:0)

首先,您可以通过android中的NavigationPageRenderer实现。

这是此演示的GIF

enter image description here

TransitionNavigationPageRender.cs

[assembly: ExportRenderer(typeof(SwipePageDemo.Controls.TransitionNavigationPage), typeof(TransitionNavigationPageRenderer))]
namespace SwipePageDemo.Droid.Renderers
{
public class TransitionNavigationPageRenderer : NavigationPageRenderer
{


    public TransitionNavigationPageRenderer(Context context) : base(context)
    {
    }



    protected override void SetupPageTransition(Android.Support.V4.App.FragmentTransaction transaction, bool isPush)
    {

                if (isPush)
                {

                }
                else
                {
                    transaction.SetCustomAnimations(Resource.Animation.enter_left, Resource.Animation.exit_right,
                                                    Resource.Animation.enter_right, Resource.Animation.exit_left);
                }

    }
}
}

enter_left.xml

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:propertyName="enter_from_left"
  android:shareInterpolator="false">
<translate
  android:fromXDelta="-100%" android:toXDelta="0%"
  android:fromYDelta="0%" android:toYDelta="0%"
  android:duration="300"/>
</set>

enter_right.xml

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
  <translate
     android:fromXDelta="100%" android:toXDelta="0%"
     android:fromYDelta="0%" android:toYDelta="0%"
     android:duration="300" />
</set>

exit_left.xml

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
 <translate
  android:fromXDelta="0%" android:toXDelta="-100%"
  android:fromYDelta="0%" android:toYDelta="0%"
  android:duration="300"/>
</set>

exit_right.xml

<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
  android:fromXDelta="0%" android:toXDelta="100%"
  android:fromYDelta="0%" android:toYDelta="0%"
  android:duration="300" />
</set>

如果您想了解有关页面过渡的更多详细信息,可以参考此链接。 https://github.com/jsuarezruiz/xamarin-forms-page-transitions

但是它仅在边缘起作用。有必要在任何地方

您可以通过threshold类的SwipeGestureRecognizer属性来实现它

The SwipeGestureRecognizer class also includes a Threshold property,可以选择将其设置为uint值,该uint值表示以独立于设备的单位识别笔划所必须达到的最小笔划距离。此属性的默认值为100,这意味着少于100个独立于设备的单位的任何轻扫都将被忽略。如果要增加可滑动区域,可以像下面的代码一样减少此阈值数据。

        var leftSwipeGesture = new SwipeGestureRecognizer { Direction = SwipeDirection.Right };
        leftSwipeGesture.Threshold = 50;
        leftSwipeGesture.Swiped += (sender, e) => Navigation.PopAsync();
       stackLayout.GestureRecognizers.Add(leftSwipeGesture);

我将leftSwipeGesture的值更改为阈值50,这是滑动的GIF。 enter image description here