xamarin.ios 仅使用图像代码实现 PageController [英] xamarin.ios implement PageController with images only code
问题描述
我正在使用 xamarin 开发一个 ios 应用程序,只有代码,没有 StoryBoard 或任何设计师.我需要实现一个包含许多图像并水平滚动的 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
,只需要添加属性Index
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
方法:
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;
}
希望能帮到你.
如果您还有一些问题,请留在这里.
If you still have some questions, just leave it here.
这篇关于xamarin.ios 仅使用图像代码实现 PageController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!