DynamicPageViewController
DynamicPageViewController copied to clipboard
Port to C#
Can you do it?
Hi, i ported this to C# myself and it works. A few things left to the side bacause i'm not an expert on Swift and it seems are not needed on Xamarin ( init(), DMDynamicPageViewControllerDelegate protocol). I still get "Index out of bounds" if i try to delete a Child when List is empty but i can handle that. Here is the link: https://gist.github.com/alexrainman/df03bd8d03a21783237d
Take a look, and maybe you can improve it.
Thank you!
Oh, cool! I appreciate that and hope people will find it useful. I'm not very good with C# so I cannot help much here, though.
It's very useful as there's not default ViewPager on iOS/Xamarin/C# so, i hope people use it :)
I should add a README file to the project and include a link to your port so that people are able to find it more easily. If that's OK with you, of course...
Of course you can :)
Hi Nik, i made a change to the port. CurrentPage wasn't updating so, i changed ScrollView Scrolled event to this:
[Foundation.Export ("scrollViewDidScroll:")] public void Scrolled (UIScrollView scrollView) { // Update the page when more than 50% of the previous/next page is visible var page = Math.Floor((containerScrollView.ContentOffset.X - pageWidth / 2) / pageWidth) + 1;
/*if (CurrentPage != (int)page) {
// Check the page to avoid "index out of bounds" exception.
if (page >= 0 && (int)page < ViewControllers.Count)
{
//self.notifyDelegateDidSwitchPage()
}
}*/
// Check whether the current view controller is fully presented.
if ((int)containerScrollView.ContentOffset.X % (int)pageWidth == 0)
{
CurrentPage = (int)page;
//FullySwitchedPage = CurrentPage;
}
}
By the way, what "index out of bounds" code does?
Hi Nik, There's something this thing needs. It needs to behave like android viewpager and recycle rendered screens. That means that only 3 screens will be rendered at a time: previous, current and next.
Can you implement it on yours so i can port it to C#?
Thanks.
maybe you can take a look at this: http://stackoverflow.com/questions/14755782/releasing-unused-pages-in-uipageviewcontroller and we can work it out together :)
That sounds like a nice idea!
And you know what's going to be a good way to achieve that? Those "lazy" variables that Swift inherits from functional programming. We can make the viewControllers array generate it's next/previous item "on-demand". That's going to be sweet.
However, I'm not sure you will be able to easily port to C#. Does it have similar functionality?
C# supports Lazy<T>, i don't know if that's the equivalent. Can you give an example?
I was referring to Swift lazy collections. But now that I think about it, it's not going to help us all that much. Maybe it would be better to just use a data source like the conventional UIPageViewController.
How can i add a listener to this thing so, when i am at the last controller, if i swipe/drag from right to left, load more items?
Take a look at this, it may help you with the loading and recycling views. The code seems to be complicated comparing with yours: https://github.com/nicklockwood/SwipeView
I was able to load more with this code:
[Foundation.Export ("scrollViewWillBeginDragging:")] public void DraggingStarted (UIKit.UIScrollView scrollView) { _contentOffsetX = scrollView.ContentOffset.X; }
[Foundation.Export ("scrollViewDidEndDragging:willDecelerate:")] public async void DraggingEnded (UIKit.UIScrollView scrollView, bool willDecelerate) { if (_contentOffsetX < scrollView.ContentOffset.X) { } }
I've pushed some changes in a new branch - dataSourceFeature. It's far from ready, but it outlines my basic idea.
K, i will take a look, Tanke
Nik,
I added this line of code to your library ViewDidLoad event to solve dragging problem when only 1 view:
containerScrollView.AlwaysBounceHorizontal = true;
I think we may create this thing inheriting from UIViewPageController that already has the view reusing functionality that we need. You may add your logic to Add/Remove views.
What do you think?
Tanke.