Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

c# - xamarin.ios implement PageController with images only code

i'm developing an ios app with xamarin, only code, without StoryBoard, or any designer. i need to implement a UiViewController that contains many images, and scrolls horizontally, just like this.

I don't have found anything suitable for me. so someone have some suggestion or some example to show me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Seems like you need a UIPageViewController without storyboard or .xib.

You need 3 custom classes to implement it.

  1. MyPageViewController - A custom 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 - A custom UIViewController, just need to add the property 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;
        }
    }
    

And finally, override the FinishedLaunching method in your AppDelegate.cs:

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;
}

Hope it can help you.

If you still have some questions, just leave it here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...