xamarin.ios使用仅图像代码实现PageController [英] xamarin.ios implement PageController with images only code
问题描述
我正在开发一个带有xamarin的ios应用程序,只有代码,没有StoryBoard或任何设计师。
i需要实现一个包含许多图像的UiViewController,并横向滚动,就像这个。
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?
推荐答案
好像你需要一个 UIPageViewController
没有storyboard或.xib。
Seems like you need a UIPageViewController
without storyboard or .xib.
您需要3个自定义类来实现它。
You need 3 custom classes to implement it.
-
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);
}
}
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;
}
}
ContentViewController
- 自定义 UIViewController
,只需添加属性索引
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;
}
}
最后,覆盖 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;
}
希望它可以帮到你。
如果您还有一些问题,请将其保留在此处。
If you still have some questions, just leave it here.
这篇关于xamarin.ios使用仅图像代码实现PageController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!