xamarin.ios使用仅图像代码实现PageController

本文关键字:图像代码 实现 PageController ios xamarin | 更新日期: 2023-09-27 18:01:04

我正在用xamarin开发一个ios应用程序,只有代码,没有StoryBoard,也没有任何设计器。我需要实现一个包含许多图像并水平滚动的UiViewController,就像这样。

我没有找到适合我的东西。所以有人给我一些建议或例子吗?

xamarin.ios使用仅图像代码实现PageController

似乎您需要一个没有故事板或.xib.的UIPageViewController

你需要3个自定义类来实现它

  1. MyPageViewController-自定义UIPageViewController

    public class MyPageViewController : UIPageViewController
    {
        private List<ContentViewController> pages = new List<ContentViewController>();
        public MyPageViewController() : base(UIPageViewControllerTransitionStyle.Scroll, UIPageViewControllerNavigationOrientation.Horizontal)
        {
            View.Frame = UIScreen.MainScreen.Bounds;
            pages.Add(new ContentViewController(0,UIColor.Red));
            pages.Add(new ContentViewController(1,UIColor.Green));
            pages.Add(new ContentViewController(2,UIColor.Blue));
            DataSource = new PageDataSource(pages);
            SetViewControllers(new UIViewController[] { pages [0] as UIViewController }, UIPageViewControllerNavigationDirection.Forward, false, null);
        }
    }
    
  2. PageDataSource:

    public class PageDataSource : UIPageViewControllerDataSource
    {
        List<ContentViewController> pages; 
        public PageDataSource(List<ContentViewController> pages)
        {
            this.pages = pages;
        }
        override public UIViewController GetPreviousViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
        {
            ContentViewController currentPage = referenceViewController as ContentViewController;
            ContentViewController pageToReturn = null;
            if (currentPage.Index == 0)
            {
                pageToReturn = pages[pages.Count - 1];
            }
            else
            {
                pageToReturn = pages[currentPage.Index - 1];
            }
            // NOTE: If the same view controller is returned, UIPageViewController will break and show black screen
            return pageToReturn != currentPage ? pageToReturn : null; 
        }
        override public UIViewController GetNextViewController(UIPageViewController pageViewController, UIViewController referenceViewController)
        {
            ContentViewController currentPage = referenceViewController as ContentViewController;
            ContentViewController pageToReturn = pages[(currentPage.Index + 1) % pages.Count];
            return pageToReturn != currentPage ? pageToReturn : null;
        }
    }
    
  3. ContentViewController-自定义UIViewController,只需添加属性Index

    public class ContentViewController : UIViewController
    {
        private int index = -1;
        public int Index
        {
            get
            { 
                return index;
            }
        }
        public ContentViewController(int _index, UIColor backColor)
        {
            this.index = _index;
            this.View.Frame = UIScreen.MainScreen.Bounds;
            this.View.BackgroundColor = backColor;
        }
    }
    

最后,覆盖AppDelegate.cs:中的FinishedLaunching方法

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    this.Window = new UIWindow(UIScreen.MainScreen.Bounds);
    this.Window.RootViewController = new MyPageViewController();
    this.Window.MakeKeyAndVisible();
    return true;
}

希望它能帮助你。

如果你还有一些问题,就把它留在这里。