IOS development of a multi view page (can share the same page) around the sliding switch to achieve a simple view

IOS development of a multi view page (can share the same page) around the sliding switch to achieve a simple view
multi view page slide back and forth around the effect of switching

more and more clients will encounter the need to achieve a similar “headline” client like multi view page sliding back and forth around.
so today we will package this function to achieve this function.

First look at the requirements:
1, click the title button to switch to the corresponding table view, or left and right sliding view slide to the corresponding view.
2, can share a controller, can realize different controller.

There are a lot of things on the Internet how many titles there are a number of controllers, and then add for to the array, the array into the creation.
so,
today we package, it will not have so much trouble, you can share a controller. Meet a lot of clients only request parameters are not the same, but the page is a long.

IOS development of a multi view page (can share the same page) around the sliding switch to achieve a simple view
sliding switch view

See the package directly below:

/ / / / XLBasePageController.h / / XLPackKnowledge / / Created / / by apple on 16/12/7. Copyright apple. All rights of 2016 / / reserved. / / #import < UIKit/UIKit.h> @class XLBasePageController; #pragma mark View Pager Delegate @protocol XLBasePageControllerDelegate < NSObject> @optional; / / / return current view controller display - (void) viewPagerViewController: (XLBasePageController * viewPager) didFinishScrollWithCurrentViewController: (UIViewController * viewController) returns the current view controller; / / / will slide - (void) viewPagerViewController: (XLBasePageController * viewPager) willScrollerWithCurrentViewController: (UIViewController *) ViewController @end #pragma mark View Pager; DataSource @protocol XLBasePageControllerDataSou Rce < NSObject> @required - (NSInteger) numberViewControllersInViewPager: (XLBasePageController * viewPager); - (UIViewController *) viewPager: (* XLBasePageController) viewPager indexViewControllers: (NSInteger) index; - (NSString *) viewPager: (* XLBasePageController) viewPager titleWithIndexViewControllers: index @optional (NSInteger); / / / set the controller Title button style, not set as the default - (UIButton *) viewPager: (* XLBasePageController) viewPager titleButtonStyle: (NSInteger) index; (CGFloat) - heightForTitleViewPager: (XLBasePageController * viewPager); / / / reserved data source - (UIView) headerViewForInViewPager: (XLBasePageController * viewPager); - (CGFloat) heightForHeaderViewPager: (XLBasePageController * viewPager); @end @interface XLBasePageController UIViewController: @property (nonatomic, weak) id< XLBasePageControllerDataSource> dataSource; @property (nonatomic, weak) id< XLBasePageControllerDelegate> delegate; / / / refresh - (void) reloadScrollPage; @property (nonatomic / / / default selection, assign NSInteger selectIndex); / / / the height of the font size button to underline the default color color selected @property (nonatomic, assign) CGFloat lineWidth; @property (nonatomic, strong) UIFont *titleFont @property (nonatomic, strong); UIColor *defaultColor; @property (nonatomic, strong) UIColor *chooseColor; @end #pragma mark @interface XLBasePageTitleButton UIButton @property: the title button (nonatomic, assign) CGFloat buttonlineWidth; @end
/ / / / XLBasePageController.m / / XLPackKnowledge / / Created / / by apple on 16/12/7. Copyright apple. All rights of 2016 / / reserved. / / #import XLBasePageController.h @interface XLBasePageController (<); UIPageViewControllerDelegate, UIPageViewControllerDataSource> NSInteger //VC {totalVC; the total number of NSArray *VCArray; / / VC storage array NSArray *buttonArray VC Button; / / storage array UIView / *headerView; the head of the view CGRect oldRect; / / Title Rect XLBasePageTitleButton is used to preserve the layout of *oldButton; NSInteger currentVCIndex; / / VC} @property current index (nonatomic, strong) UIPageViewController *pageViewController; @property (nonatomic, strong) UIScrollView *titleBGScroll; @end @implementation XLBasePageController (void) viewDidLoad {[super viewDidLoad]; self.lineWidth = self.lineWidth> self.lineWidth:1.5 = 0; self.titleFont? Self.titleFont? Self.titleFont:[UIFont systemFontOfSize:14.0]; self.defaultColor self.defaultColor:[UIColor blackColor]; self.chooseColor = self.defaultColor? = self.chooseColor? Self.chooseColor:[UIColor redColor] [self addChildViewController:self.pageViewController]; [self.view; addSubview: self.pageViewController.view]; [self.view addSubview:self.titleBackground];} - {if (UIPageViewController * pageViewController) (! _pageViewController {_pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPag) EViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; _pageViewController.delegate = self; _pageViewController.dataSource = self;} return _pageViewController;} - (UIScrollView * titleBackground) {if (_titleBGScroll! = [[UIScrollView) {_titleBGScroll alloc] init]; _titleBGScroll.backgroundColor [UIColor = whiteColor]; _titleBGScroll.showsHorizontalScrollIndicator = NO; _titleBGScroll.showsVerticalScrollIndicator = NO;} return _titleBGScroll;} - (void) setDataSource: (id< XLBasePageControllerDataSource> dataSource) {_dataSource = dataSource; //[self reloadScrollPage];} - (void) reloadScrol LPage {if ([self.dataSource respondsToSelector:@selector (numberViewControllersInViewPager:)]) {oldRect = CGRectZero; totalVC = [self.dataSource numberViewControllersInViewPager:self]; NSMutableArray *VCList = [NSMutableArray array]; NSMutableArray *buttonList [NSMutableArray for (int = array]; I = 0; i< totalVC; i++) {if ([self.dataSource respondsToSelector:@selector (viewPager:indexViewControllers:)] viewcontroller) {id = [self.dataSource viewPager:self indexViewControllers:i]; if ([viewcontroller isKindOfClass:[UIViewController class]]) {[VCList}} (addObject:viewcontroller]; if [sel F.dataSource respondsToSelector:@selector (viewPager:titleWithIndexViewControllers:)] *buttonTitle) {NSString = [self.dataSource viewPager:self titleWithIndexViewControllers:i] if (buttonArray.count; > I) {[[buttonArray objectAtIndex:i] removeFromSuperview]}; UIButton *button; if ([self.dataSource respondsToSelector:@selector (viewPager:titleButtonStyle:)] if ([[self.dataSource viewPager:self) {titleButtonStyle:i] isKindOfClass:[UIButton = button class]]) {viewPager:self} [self.dataSource titleButtonStyle:i];}else{XLB AsePageTitleButton *autoButton = [[XLBasePageTitleButton alloc] init]; autoButton.buttonlineWidth = self.lineWidth; [autoButton.titleLabel setFont:self.titleFont]; [autoButton setTitleColor:self.defaultColor forState:UIControlStateNormal]; autoButton setTitleColor:self.chooseColor [forState:UIControlStateSelected]; button = autoButton;} [button addTarget:self action:@selector (titleButtonClick:) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake (oldRect.origin.x+oldRect.size.width, 0, [self textString:buttonTitle withFontHeight:20], [self.dataSource respondsToSelecto R:@selector (heightForTitleViewPager:)] [self.dataSource? HeightForTitleViewPager:self]: 0); oldRect = button.frame; [button setTitle:buttonTitle forState:UIControlStateNormal]; [buttonList addObject:button]; [_titleBGScroll addSubview: button]; if (I = = self.selectIndex) {oldButton [buttonList = objectAtIndex:self.selectIndex]; oldButton.selected = YES;}}} / / when all button size is smaller than the width of the screen time to re layout of the if (buttonList.count & & ((UIButton) buttonList.lastObject (.Frame.origin.x) + (UIButton *) buttonList.lastObject.Frame.size.width< self.view.) Frame.size.width) {oldRect = CGRectZero; CGFloat = self.view.frame.size.width- (padding ((UIButton) buttonList.lastObject (.Frame.origin.x) + (UIButton *) buttonList.lastObject).Frame.size.width (XLBasePageTitleButton); for * button in buttonList) {button.frame = CGRectMake (oldRect.origin.x+oldRect.size.width, 0, button.frame.size.width+padding/buttonList.count, [self.dataSource respondsToSelector:@selector (heightForTitleViewPager:)] [self.dataSource? HeightForTitleViewPager:self]: 0); oldRect buttonArray = button.frame;}} = [buttonList = copy]; VCArray [VCList copy];} if ([self.dataSource respondsToSelector:@selector (head ErViewForInViewPager:)] removeFromSuperview]) {[headerView = [self.dataSource; headerView headerViewForInViewPager:self]; [self.view addSubview:headerView];} if {[_pageViewController (VCArray.count) setViewControllers:@[VCArray[self.selectIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];}} - (void) titleButtonClick: (XLBasePageTitleButton * sender) {oldButton.selected = NO; sender.selected = YES; oldButton = sender; NSInteger = index [buttonArray indexOfObject:sender]; [_pageViewController setViewControllers:@[VCArray[index]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; [self scrollVie WOffset:sender];} - (void) titleButtonConvert: (XLBasePageTitleButton * sender) {oldButton.selected = NO; sender.selected = YES; oldButton = sender; [self scrollViewOffset:sender];} - (void) scrollViewOffset: (UIButton * button) {if (! (_titleBGScroll.contentSize.width> CGRectGetWidth (self.view.frame)) {return}); if (CGRectGetMidX (button.frame) > CGRectGetMidX (self.view.frame)) {if (_titleBGScroll.contentSize.width< CGRectGetMaxX (self.view.frame) /2+CGRectGetMidX (button.frame) {[_titleBGScroll (_titleBGScroll.contentSize.width-CGRectGetWidth) setContentOffset:CGPointMake (self.view.frame), 0) animated:YES] else{[_titleBGScroll setContentOffset:CGPoi;} NtMake (CGRectGetMidX (button.frame) -CGRectGetWidth (self.view.frame) /2 animated:YES], 0);}} else{[_titleBGScroll setContentOffset:CGPointMake (0, 0) animated:YES] #pragma mark -UIPageViewControllerDelegate;}} - (void) pageViewController: (UIPageViewController *) pageViewController didFinishAnimating: (BOOL) finished previousViewControllers: (NSArray< UIViewController; *> *) previousViewControllers transitionCompleted: (BOOL) {completed if (completed) {if (currentVCIndex! = [VCArray indexOfObject:previousViewControllers[0]]) {[self chooseTitleIndex:currentVCIndex]; if ([self.delegate respondsToSelector:@selector (viewPagerViewController:didFinishScrollWithCurrentViewController: [self.delegate viewPagerViewController:self didFinishScrollWithCurrentViewController:[VCArray])) {objectAtIndex:currentVCIndex]]}}};} - (void) pageViewController: (UIPageViewController * pageViewController) (willTransitionToViewControllers: NSArray< UIViewController *> * pendingViewControllers) {currentVCIndex = [VCArray (indexOfObject:pendingViewControllers[0]]; if [self.delegate respondsToSelector:@selector (viewPagerViewController:willScrollerWithCurrentViewController:)])}} {[self.delegate viewPagerViewController:self willScrollerWithCurrentViewController:pageViewController.viewControllers[0]]; #pragma mark -UIPageViewControllerDataSource (nullable - UIViewControlle R * (UIPageViewController * pageViewController) pageViewController:) viewControllerBeforeViewController: (UIViewController * viewController) {NSInteger index = [VCArray indexOfObject:viewController]; if (index = 0 || index = = NSNotFound) {return nil;}else{return VCArray[--index];}} - (nullable * UIViewController) pageViewController: (UIPageViewController * pageViewController) viewControllerAfterViewController: (UIViewController * viewController) {NSInteger = index [VCArray indexOfObject:viewController]; if (index = = VCArray.count-1 || index = = NSNotFound) {return nil;}else{return VCArray[++index];}} - (void) chooseTitleIndex: (NSInteger) {index [self titleButtonConvert ButtonArray[index]] - (void);} viewDidLayoutSubviews {headerView.frame = CGRectMake (0, self.topLayoutGuide.length, self.view.frame.size.width, [self.dataSource respondsToSelector:@selector (heightForHeaderViewPager:)] [self.dataSource? HeightForHeaderViewPager:self]: 0; _titleBGScroll.frame = 0 (CGRectMake), (headerView.frame.size.height) headerView.frame.origin.y+headerView.frame.size.height:self.topLayoutGuide.length, self.view.frame.size.width, [self.dataSource? RespondsToSelector:@selector (heightForTitleViewPager

Project requirements, in the reserved head view does not slide along with the effect, if not, you can not achieve this method, the default is no head view, the effect of the map:

iOS开发之多视图页面(可共用同一个页面)左右来回滑动切换视图的简单实现
head view is fixed

So how to use it? Very simple, a new controller inheritance: XLBasePageController, and then achieve:
XLBasePageControllerDelegate, XLBasePageControllerDataSource

/ / / / BaseScrollListVC.m / / XLPackKnowledge / / Created / / by apple on 16/12/7. Copyright apple. All rights of 2016 / / reserved. / / #import "BaseScrollListVC.h" #import "ListVC.h" #import "DetailVC.h" @interface (BaseScrollListVC) < XLBasePageControllerDelegate, XLBasePageControllerDataSource> @property (nonatomic, strong) NSArray *titleArray @property (nonatomic, strong); UIView *headerView; @end @implementation BaseScrollListVC - (void) viewDidLoad viewDidLoad] self.view.backgroundColor {[super; [UIColor = whiteColor]; _titleArray = @[@ "all", "recommended", @ @ @ "hot spots", "near", "subscribe", @ @ @ "Q", "society", "Sports", "@ @ financial"] = self.delegate; self; self.dataSource = self; self.lineWidth = 2; / / Select the width of self.titleFont = [UIFont underline systemFontOfSize:16.0]; self.defaultColor [UIColor = blackColor]; self.chooseColor = [UIColor / default font color / font color selected blueColor]; self.selectIndex = 1; / / default page [self reloadScrollPage];} - (NSInteger) numberViewControllersInViewPager: (XLBasePageController * viewPager) {return _titleArray.count;} - (UIViewController * viewPager: * (XLBasePageController) viewPager indexViewControllers: index (NSInteger)) {if (index! = 2) {ListVC *listVC = [[ListVC alloc] init]; listVC.title = _titleArray[index]; listVC.index = index; return listVC;}else{DetailVC *detailVC = [[DetailVC alloc] init]; deta IlVC.title = _titleArray[index]; detailVC.index = index; return detailVC;}} - (CGFloat) heightForTitleViewPager: (XLBasePageController * viewPager) {return 50;} - (NSString *) viewPager: (* XLBasePageController) viewPager titleWithIndexViewControllers: (NSInteger) index {return self.titleArray[index];} - (void) viewPagerViewController: (XLBasePageController * viewPager) didFinishScrollWithCurrentViewController: (UIViewController *) viewController {self.title = viewController.title;} #pragma mark can not achieve reserves -- - (UIView *) headerViewForInViewPager: (* XLBasePageController) viewPager {return self.headerView;} - (CGFloat) heightForHeaderViewPager: (XLBasePageController * viewPager) {return} (UIV - 100; Iew * headerView) {if (_headerView = = Nil) {_headerView = [[UIView alloc] init]; _headerView.backgroundColor = [UIColor colorWithRed:120/255.0f green:210/255.0f blue:249/255.0f alpha:1]; UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake (0, 10, self.view.bounds.size.width, 40); label.textColor [UIColor = grayColor]; label.font = [UIFont systemFontOfSize:12.0]; label.text = @ "fixed head View, not can follow the slide, can not display; label.textAlignment = NSTextAlignmentCenter; [_headerView addSubview:label];} return _headerView;} - {(void) didReceiveMemoryWarning [super didReceiveMemoryWarning] Dispose of any resources that; / / can be recreated.} @end

IOS development of a multi view page (can share the same page) around the sliding switch to achieve a simple view
can not set, have a default value

below are a few easy to customize attributes.

Self.lineWidth = 2; / / self.titleFont = [UIFont width selected underline systemFontOfSize:16.0]; self.defaultColor [UIColor = blackColor]; self.chooseColor = [UIColor / default font color / font color selected blueColor]; self.selectIndex = 1; / / default selection in the first few pages


More and more clients will encounter the need to achieve a similar “headline” client like multi view page sliding back and forth around.

下面是几个可随便自定义的属性。

    self.lineWidth = 2.0;//选中下划线宽度
    self.titleFont = [UIFont systemFontOfSize:16.0];
    self.defaultColor = [UIColor blackColor];//默认字体颜色
    self.chooseColor = [UIColor blueColor];//选中字体颜色
    self.selectIndex = 1;//默认选中第几页