[iOS 控件]翻頁切換

實(shí)現(xiàn)如下效果:


pageView
pageView

分析

通過傳入標(biāo)題數(shù)組, 視圖控制器數(shù)組初始化 UIScrollView, 實(shí)現(xiàn)翻頁.
通過 ViewController的 addChildrenViewCrotroller 來讓各個自視圖控制器控制子視圖.

實(shí)現(xiàn)

代理方法

類似系統(tǒng) UITableView 的實(shí)現(xiàn)方式, 定義數(shù)據(jù)源,與代理方法

  • 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

主要實(shí)現(xiàn)邏輯

設(shè)置標(biāo)題

-(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];
}

設(shè)置 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++;
    }
}

源代碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,289評論 3 38
  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,726評論 1 14
  • 【R:閱讀原文片段】 《魏斯曼演講圣經(jīng)1:說的藝術(shù)》,摘錄自31~33頁 引起聽眾共鳴的關(guān)鍵,就是不斷回答一個核心...
    津聿閱讀 153評論 0 0
  • 1 陳鴻宇是歌手,代表作就是今天的《理想三旬》。 他《早春的樹》、《途中》、《來信》都不錯,可以單曲循環(huán)的聽。 第...
    秋葉大叔閱讀 1,287評論 3 7
  • 從小就愛做夢,夢里稀奇古怪,什么都有。 但是大多都是很瑣碎的夢,就像是白天的思考長了尾巴,一不留神就漏出來。 想來...
    樸擬閱讀 253評論 0 0

友情鏈接更多精彩內(nèi)容