[iOS 控件]翻页切换

实现如下效果:


pageView
pageView

分析

通过传入标题数组, 视图控制器数组初始化 UIScrollView, 实现翻页.
通过 ViewController的 addChildrenViewCrotroller 来让各个自视图控制器控制子视图.

实现

代理方法

类似系统 UITableView 的实现方式, 定义数据源,与代理方法

  • id<LUHPageViewControllerSourceDate> sourceData
@protocol LUHPageViewControllerSourceDate <NSObject>
@required
-(NSInteger)widthWithSingleTitle;
-(NSArray *)titlesForPage:(LUHPageViewController *)pageView;
-(NSArray *)contentViewControllersForPage:(LUHPageViewController *)pageView;
@end
  • id<LUHPageViewControllerDelegate> delegate
@protocol LUHPageViewControllerDelegate <NSObject>
-(void)pageDidShow:(UIViewController *)viewController;
@end

主要实现逻辑

设置标题

-(void)addTitle {
    NSArray *titles = [self.sourceData titlesForPage:self];
    NSInteger titlesWidth = [self.sourceData widthWithSingleTitle];
    
    if (self.titleStyle == LUHPageViewTitleStyleSegment) {
        _segmentControl = [[UISegmentedControl alloc] initWithItems:titles];
        _segmentControl.selectedSegmentIndex = 0;
        _segmentControl.frame = CGRectMake(0, 0, titles.count * titlesWidth, kLUHPageViewControllerTitleHeight);
        [_segmentControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        
        [self.navigationItem setTitleView:_segmentControl];
    } else if (self.titleStyle == LUHPageViewTitleStyleUnderlineButton) {
        _underLineButtonView = [[LUHUnderLineButtonView alloc] initWithItems:titles];
        _underLineButtonView.frame = CGRectMake(0, 0, titles.count * titlesWidth, kLUHPageViewControllerTitleHeight);
        [_underLineButtonView addTarget:self action:@selector(underLineButtonAction:)];
        
        [self.navigationItem setTitleView:_underLineButtonView];
    }
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:.7 green:.7 blue:.8 alpha:1];
}

设置 scrollView

-(void)addContentViewController
{
    NSArray *contents = [self.sourceData contentViewControllersForPage:self];
    _contentsView = [NSArray arrayWithArray:contents];
    int i = 0;
    for (UIViewController *page in contents) {
        [self addChildViewController:page];
        page.view.frame = CGRectMake(CGRectGetWidth(self.view.frame)*i,
                                     0, CGRectGetWidth(self.view.frame),
                                     CGRectGetHeight(self.view.frame));
        [self.scrollView addSubview:page.view];
        i++;
    }
}

源代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,977评论 3 38
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,359评论 1 14
  • 【R:阅读原文片段】 《魏斯曼演讲圣经1:说的艺术》,摘录自31~33页 引起听众共鸣的关键,就是不断回答一个核心...
    津聿阅读 1,211评论 0 0
  • 1 陈鸿宇是歌手,代表作就是今天的《理想三旬》。 他《早春的树》、《途中》、《来信》都不错,可以单曲循环的听。 第...
    秋叶大叔阅读 4,938评论 3 7
  • 从小就爱做梦,梦里稀奇古怪,什么都有。 但是大多都是很琐碎的梦,就像是白天的思考长了尾巴,一不留神就漏出来。 想来...
    朴拟阅读 1,639评论 0 0

友情链接更多精彩内容