我的UIPageViewControllerDelegate方法存在问题。当用户在一个方向上滑动时,它不会正确跟踪页面,也不会完成滑动并滑动回到第一页。
实施例
以下是我实现这些方法的方法:
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)
并且有完全相同的问题。
所以我必须在这两种方法中做错,有什么建议吗?
答案 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);
}
}
希望这有帮助!