bsbdj-XMGEssenceViewController

XMGEssenceViewController

屏幕快照 2016-05-06 下午3.25.59.png

精華頁面里有5個子控制器(全部,視頻治笨,聲音驳概,圖片粪小,段子)這5個控制器都是XMGTopicViewController,只不過其中顯示的cell的樣式是不一樣的抡句。并且這5個控制器是可以通過手勢來進(jìn)行切換的探膊,或者是通過上邊的(全部,視頻待榔,聲音逞壁,圖片,段子)的按鈕進(jìn)行點擊切換

@interface XMGEssenceViewController() <UIScrollViewDelegate>
/** 標(biāo)簽欄底部的紅色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 當(dāng)前選中的按鈕 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 頂部的所有標(biāo)簽 */
@property (nonatomic, weak) UIView *titlesView;
/** 底部的所有內(nèi)容 */
@property (nonatomic, weak) UIScrollView *contentView;
@end

-(void)viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 設(shè)置導(dǎo)航欄
    [self setupNav];

    // 初始化子控制器
    [self setupChildVces];

    // 設(shè)置頂部的標(biāo)簽欄
    [self setupTitlesView];

    // 底部的scrollView
    [self setupContentView];
}

初始化子控制器锐锣,就是那5個子控制器腌闯,

 - (void)setupChildVces
{
XMGTopicViewController *all = [[XMGTopicViewController alloc] init];
all.title = @"全部";
all.type = XMGTopicTypeAll;
[self addChildViewController:all];

XMGTopicViewController *video = [[XMGTopicViewController alloc] init];
video.title = @"視頻";
video.type = XMGTopicTypeVideo;
[self addChildViewController:video];

XMGTopicViewController *voice = [[XMGTopicViewController alloc] init];
voice.title = @"聲音";
voice.type = XMGTopicTypeVoice;
[self addChildViewController:voice];

XMGTopicViewController *picture = [[XMGTopicViewController alloc] init];
picture.title = @"圖片";
picture.type = XMGTopicTypePicture;
[self addChildViewController:picture];

XMGTopicViewController *word = [[XMGTopicViewController alloc] init];
word.title = @"段子";
word.type = XMGTopicTypeWord;
[self addChildViewController:word];
}

設(shè)置頂部的標(biāo)簽欄

- (void)setupTitlesView
{
// 標(biāo)簽欄整體
UIView *titlesView = [[UIView alloc] init];
titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
titlesView.width = self.view.width;
titlesView.height = XMGTitilesViewH;
titlesView.y =XMGTitilesViewY;
[self.view addSubview:titlesView];
self.titlesView = titlesView;

// 底部的紅色指示器
UIView *indicatorView = [[UIView alloc] init];
indicatorView.backgroundColor = [UIColor redColor];
indicatorView.height = 2;
indicatorView.tag = -1;
indicatorView.y = titlesView.height - indicatorView.height;
self.indicatorView = indicatorView;

// 內(nèi)部的子標(biāo)簽
CGFloat width = titlesView.width / self.childViewControllers.count;
CGFloat height = titlesView.height;
for (NSInteger i = 0; i<self.childViewControllers.count; i++) {
    UIButton *button = [[UIButton alloc] init];
    button.tag = i;
    button.height = height;
    button.width = width;
    button.x = i * width;
    UIViewController *vc = self.childViewControllers[i];
    [button setTitle:vc.title forState:UIControlStateNormal];
    //[button layoutIfNeeded]; // 強(qiáng)制布局(強(qiáng)制更新子控件的frame)
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
    [titlesView addSubview:button];
    
    // 默認(rèn)點擊了第一個按鈕
    if (i == 0) {
        button.enabled = NO;
        self.selectedButton = button;
        
        // 讓按鈕內(nèi)部的label根據(jù)文字內(nèi)容來計算尺寸
        [button.titleLabel sizeToFit];
        self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
    }
}

    [titlesView addSubview:indicatorView];
}

-(void)titleClick:(UIButton *)button{}

- (void)titleClick:(UIButton *)button
{
// 修改按鈕狀態(tài)
self.selectedButton.enabled = YES;
button.enabled = NO;
self.selectedButton = button;

// 動畫
[UIView animateWithDuration:0.25 animations:^{
    self.indicatorView.width = button.titleLabel.width;
    self.indicatorView.centerX = button.centerX;
}];

// 滾動
CGPoint offset = self.contentView.contentOffset;
offset.x = button.tag * self.contentView.width;
[self.contentView setContentOffset:offset animated:YES];
}

底部的scrollView

- (void)setupContentView
{
// 不要自動調(diào)整inset
self.automaticallyAdjustsScrollViewInsets = NO;

UIScrollView *contentView = [[UIScrollView alloc] init];
contentView.frame = self.view.bounds;
contentView.delegate = self;
contentView.pagingEnabled = YES;
[self.view insertSubview:contentView atIndex:0];
contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
self.contentView = contentView;

// 添加第一個控制器的view
[self scrollViewDidEndScrollingAnimation:contentView];
}

設(shè)置導(dǎo)航欄

- (void)setupNav
{
// 設(shè)置導(dǎo)航欄標(biāo)題
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];

// 設(shè)置導(dǎo)航欄左邊的按鈕
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(tagClick)];

// 設(shè)置背景色
self.view.backgroundColor = XMGGlobalBg;
}

-(void)tagClick

- (void)tagClick
{
XMGRecommendTagsViewController *tags = [[XMGRecommendTagsViewController alloc] init];
[self.navigationController pushViewController:tags animated:YES];
}

#pragma mark - <UIScrollViewDelegate>

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
// 當(dāng)前的索引
NSInteger index = scrollView.contentOffset.x / scrollView.width;

// 取出子控制器
UIViewController *vc = self.childViewControllers[index];
vc.view.x = scrollView.contentOffset.x;
vc.view.y = 0; // 設(shè)置控制器view的y值為0(默認(rèn)是20)
vc.view.height = scrollView.height; // 設(shè)置控制器view的height值為整個屏幕的高度(默認(rèn)是比屏幕高度少個20)
[scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self scrollViewDidEndScrollingAnimation:scrollView];

// 點擊按鈕
NSInteger index = scrollView.contentOffset.x / scrollView.width;
[self titleClick:self.titlesView.subviews[index]];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市雕憔,隨后出現(xiàn)的幾起案子姿骏,更是在濱河造成了極大的恐慌,老刑警劉巖斤彼,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件分瘦,死亡現(xiàn)場離奇詭異,居然都是意外死亡琉苇,警方通過查閱死者的電腦和手機(jī)嘲玫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來并扇,“玉大人去团,你說我怎么就攤上這事∏钣迹” “怎么了土陪?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長肴熏。 經(jīng)常有香客問我鬼雀,道長,這世上最難降的妖魔是什么扮超? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任取刃,我火速辦了婚禮,結(jié)果婚禮上出刷,老公的妹妹穿的比我還像新娘璧疗。我一直安慰自己,他們只是感情好馁龟,可當(dāng)我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布崩侠。 她就那樣靜靜地躺著,像睡著了一般坷檩。 火紅的嫁衣襯著肌膚如雪却音。 梳的紋絲不亂的頭發(fā)上改抡,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天驾诈,我揣著相機(jī)與錄音业栅,去河邊找鬼仰挣。 笑死腥放,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的审残。 我是一名探鬼主播先紫,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼衩茸,長吁一口氣:“原來是場噩夢啊……” “哼骗绕!你這毒婦竟也來了藐窄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤酬土,失蹤者是張志新(化名)和其女友劉穎荆忍,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體撤缴,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡刹枉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了腹泌。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嘶卧。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖凉袱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情侦铜,我是刑警寧澤专甩,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站钉稍,受9級特大地震影響涤躲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜贡未,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一种樱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧俊卤,春花似錦嫩挤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至狠怨,卻和暖如春约啊,著一層夾襖步出監(jiān)牢的瞬間邑遏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工恰矩, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留记盒,地道東北人。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓外傅,卻偏偏與公主長得像纪吮,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子栏豺,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,678評論 2 354

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,095評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫彬碱、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,098評論 4 62
  • 硬筆書法作品自作詩文四首 姓名:盧春成 院系:生命科學(xué)技術(shù)學(xué)院 聯(lián)系方式:15736959606
    墨泉書齋閱讀 352評論 8 9
  • 1 那天和兩個95后同桌吃飯奥洼,聊起另一個相識的人是82年的巷疼,不成想其中一個男孩瞪大了眼睛,語氣夸張的大聲說:“82...
    素小絹閱讀 842評論 0 0
  • 鑲著一圈假珍珠的相框 里面的相片已泛黃 散發(fā)著舊味的校服上 還有你畫的叮當(dāng)貓 守在你家門前那顆梧桐樹 陪我一起抵擋...
    汽水L閱讀 556評論 0 0