对于UIPageViewController,DidFinishAnimating和WillTransition,我该如何正确实现这些方法?

时间:2017-06-14 19:09:25

标签: c# ios iphone xamarin uipageviewcontroller

我的UIPageViewControllerDelegate方法存在问题。当用户在一个方向上滑动时,它不会正确跟踪页面,也不会完成滑动并滑动回到第一页。

实施例

  1. 我在第1页,我从右向左滑动
  2. 当我滑动时,我看到了第2页。但是,在“第1页到第2页”转换完成之前,我停止向该方向滑动并从左向右滑动(不抬起手指)。
  3. 现在我在第1页,但我看到的所有内容都与第2页排成一行。
  4. 以下是我实现这些方法的方法:

    public Action<int> PageSwiped { get; set; }
    
    public override void WillTransition(
        UIPageViewController pageViewController, UIViewController[] pendingViewControllers)
    {
        _currentController = pendingViewControllers[0] as AssetViewController;
    }
    
    public override void DidFinishAnimating(
        UIPageViewController pageViewController, bool finished,
        UIViewController[] previousViewControllers, bool completed)
    {
        if (completed)
        {
            PageSwiped(_currentController.CurrentMedia.Content.ProofIndex);
        }
    }
    

    触发'PageSwiped'操作以允许其他类知道页面被刷过。

    我尝试过替换:

    if (completed)
    

    if (finished)
    

    并且有完全相同的问题。

    所以我必须在这两种方法中做错,有什么建议吗?

1 个答案:

答案 0 :(得分:1)

使用UIPageViewController时,您不需要实现这些方法在页面之间导航(ViewControllers)。

您应该在IUIPageViewControllerDataSource中实现UIPageViewController接口,并将数据源设置为this或子类UIPageViewControllerDataSource,并将数据源设置为创建的对象。

在任何一种情况下,您都将实施/覆盖这两种方法

UIViewController IUIPageViewControllerDataSource.GetNextViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
{
}

UIViewController IUIPageViewControllerDataSource.GetPreviousViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
{
}

这些是返回将要显示的Page(UIViewController)的方法。

在这里,您只需找到当前屏幕上控件的索引,然后获取下一个或上一个,并从集合中返回该项目。

获取下一个ViewController:

UIViewController IUIPageViewControllerDataSource.GetNextViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
{
    var index = MyViewControllers.IndexOf(referenceViewController);

    var nextIndex = index + 1;

    return MyViewControllers.ElementAtOrDefault(nextIndex);
}

获取上一个ViewController:

UIViewController IUIPageViewControllerDataSource.GetPreviousViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
{
    var index = MyViewControllers.IndexOf(referenceViewController);

    var previousIndex = index - 1;

    return MyViewControllers.ElementAtOrDefault(previousIndex);
}

不同之处在于只有在转换成功完成后才会调用这些方法。

以下是完整的代码:

public partial class MyPageViewController : UIPageViewController, IUIPageViewControllerDataSource
{
    List<UIViewController> MyViewControllers = new List<UIViewController>();

    public MyPageViewController (IntPtr handle) : base (handle)
    {

    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        var redViewController = UIStoryboard.FromName("Main", null).InstantiateViewController("redViewController");
        var greenViewController = UIStoryboard.FromName("Main", null).InstantiateViewController("greenViewController");
        var blueViewController = UIStoryboard.FromName("Main", null).InstantiateViewController("blueViewController");

        MyViewControllers.Add(redViewController);
        MyViewControllers.Add(greenViewController);
        MyViewControllers.Add(blueViewController);

        DataSource = this;

        SetViewControllers(new UIViewController[] { MyViewControllers.First() }, UIPageViewControllerNavigationDirection.Forward, true, null);

    }

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
    }

    UIViewController IUIPageViewControllerDataSource.GetNextViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
    {
        var index = MyViewControllers.IndexOf(referenceViewController);

        var nextIndex = index + 1;

        return MyViewControllers.ElementAtOrDefault(nextIndex);
    }

    UIViewController IUIPageViewControllerDataSource.GetPreviousViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
    {
        var index = MyViewControllers.IndexOf(referenceViewController);

        var previousIndex = index - 1;

        return MyViewControllers.ElementAtOrDefault(previousIndex);
    }
}

希望这有帮助!