iOS實(shí)現(xiàn)一個(gè)簡單的頁面切換控件

背景

項(xiàng)目需要開發(fā)一個(gè)游戲類型的殼版本,殼版本的UI已經(jīng)出來了,剩下的事情就是復(fù)制粘貼了,重復(fù)的工作太沒意思了,不如來點(diǎn)有挑戰(zhàn)的,把項(xiàng)目重構(gòu)一遍吧,順便復(fù)制粘貼下,這樣才不會(huì)那么枯燥無聊吧。好吧,說干就干,誰叫我姓張呢?項(xiàng)目重構(gòu)的地方有很多,這次會(huì)講到一個(gè)經(jīng)常用到的頁面切換控件的重構(gòu)以及實(shí)現(xiàn)一個(gè)簡單的Demo,后續(xù)會(huì)放出一些其他方面重構(gòu)的實(shí)踐經(jīng)驗(yàn)。

結(jié)果

項(xiàng)目Demo:PTPageKit
老生常談沒圖沒真相,還是先上圖:

Demo1

Demo2

圖片很挫,大概做的就是這么一個(gè)東西,上面的切換按鈕,下面的是按鈕對(duì)應(yīng)的頁面。點(diǎn)擊按鈕,下面的頁面會(huì)隨之變化。

分析

把系統(tǒng)分為三部分,Page 頭部Page 內(nèi)容, Page 中間者,每一部分對(duì)應(yīng)的對(duì)輸入和輸出進(jìn)行抽象,對(duì)各個(gè)部分進(jìn)行組件化。

Header分析

Header的輸入(公有接口部分):

  • 選中Header Item
  • 刷新Header視圖
  • 滾動(dòng)到Header某個(gè)位置

Header的輸出(Delegate和DataSource部分):

  • 已經(jīng)選中Header Item
  • Header Item的數(shù)量
  • Header Item展示的View
  • Header Item的元數(shù)據(jù)

Body分析

Body的輸入(公有接口部分):

  • 選中Body頁面
  • 刷新Body視圖

Body的輸出(Delegate和DataSource部分):

  • 已經(jīng)滾動(dòng)到某個(gè)Body
  • Body移動(dòng)到某個(gè)位置
  • 有多少個(gè)Body
  • Body對(duì)應(yīng)的ViewController

中間者分析

中間者作為了Body和Header的Delegate和DataSource,為Body和Header提供數(shù)據(jù)并且連接了這兩者。中間者隱藏了實(shí)現(xiàn)的細(xì)節(jié),客戶端(使用者)最終只要和中間者交互即可,傳遞需要的參數(shù)交給中間者去處理,中間者會(huì)做好子元素的UI渲染和UI交互。

組件圖

下面是對(duì)這個(gè)系統(tǒng)的輸入輸出進(jìn)行抽象的組件圖。


組件圖

Page 頭部Page 內(nèi)容, Page 中間者 這三者依賴于抽象基類,沒有依賴具體的實(shí)現(xiàn)類,所以他們是相互獨(dú)立并且可替換的。對(duì)于某個(gè)具體的實(shí)現(xiàn),客戶端(使用者)只要和 Page 中間者 這個(gè)對(duì)象打交道,傳遞需要的參數(shù):頁面的Header元數(shù)據(jù)和頁面的ViewContoller數(shù)組即可,

實(shí)現(xiàn)

抽象的基類

抽象的基類是針對(duì)組件圖的交互實(shí)現(xiàn)了一個(gè)基本的框架,并不包含任何的UI實(shí)現(xiàn),定義了組件交互的規(guī)范。

Body

//
//  PTBasePageBodyView.h
//  PlushGame
//
//  Created by aron on 2017/11/23.
//  Copyright ? 2017年 aron. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol PTPageBodyViewDelegate, PTPageBodyViewDataSource;

@interface PTBasePageBodyView : UIView

@property (nonatomic, weak) id<PTPageBodyViewDelegate> delegate;
@property (nonatomic, weak) id<PTPageBodyViewDataSource> dataSource;

- (void)moveToIndex:(NSInteger)index;

- (void)reloadData;

@end



//______________________________________________________________________________________________________________

@protocol PTPageBodyViewDelegate <NSObject>

- (void)bodyView:(PTBasePageBodyView *)bodyView didSelectItemAtIndex:(NSInteger)index;
- (void)bodyView:(PTBasePageBodyView *)bodyView didScrollWithOffset:(CGFloat)offset;

@end


//_______________________________________________________________________________________________________________

@protocol PTPageBodyViewDataSource <NSObject>

@required

- (NSInteger)numberOfItemsInBodyView:(PTBasePageBodyView *)bodyView;
- (UIViewController *)bodyView:(PTBasePageBodyView *)bodyView viewControllerAtIndex:(NSInteger)index;

@end

Header

//
//  PTBasePageHeaderView.h
//  PlushGame
//
//  Created by aron on 2017/11/23.
//  Copyright ? 2017年 aron. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, PTPageHeaderStyle) {
    PTPageHeaderStyleDefault = 0,
};

@protocol PTPageHeaderViewDelegate, PTPageHeaderViewDataSource;

@interface PTBasePageHeaderView : UIView

@property (assign, nonatomic) PTPageHeaderStyle headerStyle;
@property (nonatomic, weak) id<PTPageHeaderViewDelegate> delegate;
@property (nonatomic, weak) id<PTPageHeaderViewDataSource> dataSource;

- (void)moveToIndex:(NSInteger)index;
- (void)animateMoveToOffset:(CGFloat)offset;

- (void)reloadData;

- (NSInteger)currentIndex;

@end


//______________________________________________________________________________________________________________

@protocol PTPageHeaderViewDelegate <NSObject>

- (void)headerView:(PTBasePageHeaderView *)headerView didSelectItemAtIndex:(NSInteger)index;

@end


//_______________________________________________________________________________________________________________

@protocol PTPageHeaderViewDataSource <NSObject>

@required

- (NSInteger)numberOfItemsInHeaderView:(PTBasePageHeaderView *)headerView;

@optional
- (UIView *)pageView:(PTBasePageHeaderView *)headerView headerItemViewAtIndex:(NSInteger)index;
- (id)pageView:(PTBasePageHeaderView *)headerView headerItemAtIndex:(NSInteger)index;

@end

中間者
Body.h

//
//  PTBasePageViewController.h
//  PlushGame
//
//  Created by aron on 2017/11/23.
//  Copyright ? 2017年 aron. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "PTBasePageHeaderView.h"
#import "PTBasePageBodyView.h"


@protocol PTBasePageViewDataSource <NSObject>

// 需要添加到ViewController的View上,默認(rèn)的是self.view
- (UIView*)pt_container;

@end

@interface PTBasePageViewController : UIViewController
<PTPageHeaderViewDelegate, PTPageHeaderViewDataSource,
PTPageBodyViewDelegate, PTPageBodyViewDataSource>


@property (nonatomic, weak) id<PTBasePageViewDataSource> dataSource;

@property (nonatomic, strong) PTBasePageHeaderView *headerView;
@property (strong, nonatomic) PTBasePageBodyView *bodyView;

@property (nonatomic, strong) NSArray<id>* pageHeaderTitles;
@property (nonatomic, strong) NSArray<UIViewController*>* pageBodyViewControllers;
@property (nonatomic, assign) NSInteger defaultIndex;

- (void)reloadData;

- (void)preViewDidLoadContainer;
- (UIView*)container;

@end

Body.m

//
//  PTBasePageViewController.m
//  PlushGame
//
//  Created by aron on 2017/11/23.
//  Copyright ? 2017年 aron. All rights reserved.
//

#import "PTBasePageViewController.h"

@interface PTBasePageViewController ()

@end

@implementation PTBasePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self preViewDidLoadContainer];
    [self.container addSubview:self.bodyView];
    [self.container addSubview:self.headerView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadData {
    [self.headerView reloadData];
    [self.bodyView reloadData];
}

- (void)preViewDidLoadContainer {
    
}

- (UIView*)container {
    UIView* container = nil;
    if ([self.dataSource respondsToSelector:@selector(pt_container)]) {
        container = [self.dataSource pt_container];
    } else {
        container = self.view;
    }
    return container;
}

#pragma mark - ......::::::: Lazy load :::::::......

- (PTBasePageHeaderView *)headerView {
    if (nil == _headerView) {
        _headerView = [[PTBasePageHeaderView alloc] init];
        _headerView.dataSource = self;
        _headerView.delegate = self;
    }
    return _headerView;
}

- (PTBasePageBodyView *)bodyView {
    if (nil == _bodyView) {
        _bodyView = [[PTBasePageBodyView alloc] init];
        _bodyView.dataSource = self;
        _bodyView.delegate = self;
    }
    return _bodyView;
}


#pragma mark - ......::::::: PTPageHeaderViewDelegate, PTPageHeaderViewDataSource :::::::......

- (void)headerView:(PTBasePageHeaderView *)headerView didSelectItemAtIndex:(NSInteger)index {
    [self.bodyView moveToIndex:index];
}

- (NSInteger)numberOfItemsInHeaderView:(PTBasePageHeaderView *)headerView {
    return self.pageHeaderTitles.count;
}

- (UIView *)pageView:(PTBasePageHeaderView *)headerView headerItemViewAtIndex:(NSInteger)index {
    return nil;
}

- (id)pageView:(PTBasePageHeaderView *)headerView headerItemAtIndex:(NSInteger)index {
    return self.pageHeaderTitles[index];
}

#pragma mark - ......::::::: PTPageBodyViewDelegate, PTPageBodyViewDataSource :::::::......

- (void)bodyView:(PTBasePageBodyView *)bodyView didSelectItemAtIndex:(NSInteger)index {
    [self.headerView moveToIndex:index];
}

- (void)bodyView:(PTBasePageBodyView *)bodyView didScrollWithOffset:(CGFloat)offset {
    [self.headerView animateMoveToOffset:offset];
}

- (NSInteger)numberOfItemsInBodyView:(PTBasePageBodyView *)bodyView {
    return self.pageBodyViewControllers.count;
}

- (UIViewController *)bodyView:(PTBasePageBodyView *)bodyView viewControllerAtIndex:(NSInteger)index {
    if (index >= 0 && index < self.pageBodyViewControllers.count) {
        return self.pageBodyViewControllers[index];
    }
    return nil;
}
@end

具體的實(shí)現(xiàn)

具體的實(shí)現(xiàn)需要繼承 PTBasePageBodyView PTBasePageHeaderView PTBasePageViewController 實(shí)現(xiàn)一些UI和交互的細(xì)節(jié),比如Header中按鈕的布局和樣式等。可以參考項(xiàng)目中的一個(gè)子模塊 PTSimplePageKitImpl,打開 Example Demo項(xiàng)目既可以看到效果,具體的實(shí)現(xiàn)不在贅述。

后記

項(xiàng)目Demo:PTPageKit

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,117評(píng)論 6 537
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,860評(píng)論 3 423
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,128評(píng)論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,291評(píng)論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,025評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,421評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,477評(píng)論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,642評(píng)論 0 289
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,177評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,970評(píng)論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,157評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,717評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,410評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,821評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,053評(píng)論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,896評(píng)論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,157評(píng)論 2 375

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,804評(píng)論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,682評(píng)論 25 708
  • 古銅色的陳鐘呆呆的在天臺(tái)佇立 沉默的敲鐘人 帶著卡西莫多般的面龐 遙望著遠(yuǎn)方墓地里的野花 鐘錘激起的灰塵在瘋狂的舞...
    林荔閱讀 167評(píng)論 0 2
  • 被問了一個(gè)很敏感的問題,合情合理地為自己辯解,滔滔不絕又邏輯清晰。 可,哪個(gè)才是真的我。 每天的勤懇、上進(jìn),占據(jù)著...
    記錄一些情緒閱讀 165評(píng)論 0 0
  • 最近在整理課題組編寫的書稿,每個(gè)人負(fù)責(zé)一個(gè)章節(jié),而我和Z同學(xué)負(fù)責(zé)統(tǒng)稿。下午將老板對(duì)G修改的第十章意見反饋給他,...
    鳳洹學(xué)寫作閱讀 859評(píng)論 2 0