iOS - load and switch between pages using UIPageContol

半腔热情 提交于 2019-12-06 17:24:54
Wain

Adding the other view controllers as children just associates the view controllers - it doesn't add the views they manage as subviews. So, when you add them as children you should also add the subviews and set the initial frames. You might also want to add swipe gestures to trigger view animations to animate the frames of the views (though there are several other options, like using a scroll view).

As part of the animation to move the views you manually update the page control (that won't happen automatically).


The loadScrollViewWithPage method is the key. Call this with parameter 0 and then 1 to load both of the pages and add the views as subviews. Just change self.scrollView to self.view.

I would add a (two really, but get 1 working first) swipe gesture to the page control. Then when it is swiped you can move the other views.

The swipe gesture callback:

- (void)swipeRightToLeftGesture:(UIGestureRecognizer *)gestureRecognizer
{
    [UIView animateWithDuration:1
                     animations:^{

                     for (UIViewController *vc in self.childViewControllers) {
                         CGRect frame = vc.view.frame;
                         frame.origin.x -= self.view.frame.size.width;
                         vc.view.frame = frame;
                     }
                 }];
}

This iterations over the child view controllers and animates the frames of their views to move them to the left. The swipe left to right handler will be the same except it will += the width to move the to the right.

[Code Edited - syntax]

UIPageControl doesn't handle swiping or page switching for you. In order to use it, you NEED to have a scroll view (or some other kind of control) to handle the swipes and the actual page switching, and then implement the UIScrollViewDelegate protocol (or whatever protocol you need for the control you're using) to know when the page control should update it's current page position (the currently selected dot). UIPageControl itself doesn't really do a whole lot.

UIPageControl does not do anything much except display some dots. If you want a way of switching between view controllers it sounds like you want UIPageViewController, which is a completely different animal. A nice feature of UIPageViewController is that it can display a UIPageControl as a way of indicating / switching view controllers, which may be just the kind of thing you're after.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!