結(jié)合WMPageController實(shí)現(xiàn)懸停菜單

需求

最近項(xiàng)目有一個(gè)需求渗柿,涉及到在UIScrollView嵌套的使用个盆,且里面的UIScrollView實(shí)際上是分頁(yè)控制器。需求類似于下圖:

最終效果圖.gif

其實(shí)實(shí)現(xiàn)的方案有很多種。思路都是一樣的颊亮,就是判斷通過(guò)是否懸停來(lái)判斷哪一個(gè)UIScrollView需要滾動(dòng)柴梆。

我的實(shí)現(xiàn)思路:

我的實(shí)現(xiàn)是直接使用了第三方分頁(yè)菜單控制器WMPageController。使用過(guò)WMPageController的童鞋可以往下看终惑,沒(méi)使用過(guò)得也可以去試一下绍在,個(gè)人感覺(jué)比較好用,如下圖:

菜單.png

這一部分可以直接使用WMPageController來(lái)集成狠鸳,我們只需要在外面套一層UIScrollView即可揣苏。

開(kāi)擼

1、自定義一個(gè)ArtScrollView繼承自UIScrollView件舵,重寫代理方法卸察。
#import "ArtScrollView.h"

@implementation ArtScrollView

//返回YES,則可以多個(gè)手勢(shì)一起觸發(fā)方法铅祸,返回NO則為互斥(比如外層UIScrollView名為mainScroll內(nèi)嵌的UIScrollView名為subScroll坑质,當(dāng)我們拖動(dòng)subScroll時(shí),mainScroll是不會(huì)響應(yīng)手勢(shì)的(多個(gè)手勢(shì)默認(rèn)是互斥的)临梗,
//當(dāng)下面這個(gè)代理返回YES時(shí)涡扼,subScroll和mainScroll就能同時(shí)響應(yīng)手勢(shì),同時(shí)滾動(dòng)盟庞,這符合我們這里的需求)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

@end

該ArtScrollView用在MainViewController中吃沪,解決UIScrollView嵌套的手勢(shì)沖突問(wèn)題。

2什猖、創(chuàng)建一個(gè)控制器作為容器
#import "ViewController.h"
#import "WMPageController.h"
#import "ArtScrollView.h"
#import "ChildTableViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) WMPageController *pageController;
@property (nonatomic, strong) ArtScrollView *containerScrollView;
@property (nonatomic, strong) UIView *bannerView;
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabView;
@property (nonatomic, assign) BOOL isTopIsCanNotMoveTabViewPre;
@property (nonatomic, assign) BOOL canScroll;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"標(biāo)題";
    _canScroll = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kHomeLeaveTopNotification object:nil];
    [self setupView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.alpha = 0;
}

- (void)setupView {
    [self.view addSubview:self.containerScrollView];
    [self.containerScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(self.view).offset(0);
    }];
    [self.containerScrollView addSubview:self.bannerView];
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.containerScrollView);
        make.width.equalTo(self.containerScrollView);
        make.height.mas_equalTo(200);
    }];
    [self.containerScrollView addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bannerView.mas_bottom);
        make.leading.trailing.bottom.equalTo(self.containerScrollView);
        make.width.equalTo(self.containerScrollView);
        make.height.mas_equalTo(kScreenHeight-64);
    }];
    [self.contentView addSubview:self.pageController.view];
    self.pageController.viewFrame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-64);
}

#pragma mark - getter

- (ArtScrollView *)containerScrollView {
    if (!_containerScrollView) {
        _containerScrollView = [[ArtScrollView alloc] init];
        _containerScrollView.delegate = self;
        _containerScrollView.showsVerticalScrollIndicator = NO;
    }
    return _containerScrollView;
}

- (UIView *)bannerView {
    if (!_bannerView) {
        _bannerView = [[UIView alloc] init];
        _bannerView.backgroundColor = [UIColor blueColor];
    }
    return _bannerView;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor yellowColor];
    }
    return _contentView;
}

- (WMPageController *)pageController {
    if (!_pageController) {
        _pageController = [[WMPageController alloc] initWithViewControllerClasses:@[[ChildTableViewController class],[ChildTableViewController class],[ChildTableViewController class]] andTheirTitles:@[@"tab1",@"tab2",@"tab3"]];
        _pageController.menuViewStyle      = WMMenuViewStyleLine;
        _pageController.menuHeight         = 44;
        _pageController.progressWidth      = 20;
        _pageController.titleSizeNormal    = 15;
        _pageController.titleSizeSelected  = 15;
        _pageController.titleColorNormal   = [UIColor grayColor];
        _pageController.titleColorSelected = [UIColor blueColor];
    }
    return _pageController;
}

#pragma mark - notification

-(void)acceptMsg:(NSNotification *)notification{
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if ([canScroll isEqualToString:@"1"]) {
        _canScroll = YES;
    }
}

#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat maxOffsetY = 136;
    CGFloat offsetY = scrollView.contentOffset.y;
    self.navigationController.navigationBar.alpha = offsetY/136;
    if (offsetY>=maxOffsetY) {
        scrollView.contentOffset = CGPointMake(0, maxOffsetY);
        //NSLog(@"滑動(dòng)到頂端");
        [[NSNotificationCenter defaultCenter] postNotificationName:kHomeGoTopNotification object:nil userInfo:@{@"canScroll":@"1"}];
        _canScroll = NO;
    }else{
        //NSLog(@"離開(kāi)頂端");
        if (!_canScroll) {
            scrollView.contentOffset = CGPointMake(0, maxOffsetY);
        }
    }
}

// 移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

3票彪、為WMPageController創(chuàng)建子控制器。
#import "ChildTableViewController.h"

@interface ChildTableViewController ()<UIScrollViewDelegate>
@property (nonatomic, assign) BOOL canScroll;
@end

@implementation ChildTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellid"];
    // add notification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kHomeGoTopNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:kHomeLeaveTopNotification object:nil];//其中一個(gè)TAB離開(kāi)頂部的時(shí)候不狮,如果其他幾個(gè)偏移量不為0的時(shí)候降铸,要把他們都置為0
}

#pragma mark - notification

-(void)acceptMsg:(NSNotification *)notification{
    NSString *notificationName = notification.name;
    if ([notificationName isEqualToString:kHomeGoTopNotification]) {
        NSDictionary *userInfo = notification.userInfo;
        NSString *canScroll = userInfo[@"canScroll"];
        if ([canScroll isEqualToString:@"1"]) {
            self.canScroll = YES;
            self.tableView.showsVerticalScrollIndicator = YES;
        }
    }else if([notificationName isEqualToString:kHomeLeaveTopNotification]){
        self.tableView.contentOffset = CGPointZero;
        self.canScroll = NO;
        self.tableView.showsVerticalScrollIndicator = NO;
    }
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (!self.canScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY<0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kHomeLeaveTopNotification object:nil userInfo:@{@"canScroll":@"1"}];
    }
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

總結(jié):

這個(gè)方案結(jié)合了比較流行的WMPageController實(shí)現(xiàn)了懸停菜單的功能,并且使用了通知方式來(lái)解耦MainViewController和WMPageController的子控制器摇零,相當(dāng)于對(duì)WMPageController的擴(kuò)展吧推掸。

1、遇到的坑

這里面有一個(gè)WMPageController的坑驻仅,開(kāi)始本來(lái)想的是把WMPageController的view通過(guò)約束添加到MainViewController中來(lái)谅畅,作為MainViewController的子控制器,結(jié)果不起作用噪服,原因是WMPageController內(nèi)部默認(rèn)將WMPageController視圖的frame設(shè)置成了相對(duì)window毡泻,不過(guò)可以通過(guò)設(shè)置WMPageController的viewFrame來(lái)設(shè)置frame。

2芯咧、難點(diǎn)

難點(diǎn)主要還是對(duì)手勢(shì)的理解和掌握牙捉,還有UIScrollVeiw代理的使用竹揍。

對(duì)iOS手勢(shì)基本知識(shí)還不太理解的可以參考這篇文字iOS UIGestureRecognizer (手勢(shì)的基本知識(shí)介紹)

最后附上Demo地址
如果大家也正在做這一方面的功能,希望能對(duì)你們有所幫助邪铲。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末芬位,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子带到,更是在濱河造成了極大的恐慌昧碉,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件揽惹,死亡現(xiàn)場(chǎng)離奇詭異被饿,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)搪搏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門狭握,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人疯溺,你說(shuō)我怎么就攤上這事论颅。” “怎么了囱嫩?”我有些...
    開(kāi)封第一講書人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵恃疯,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我墨闲,道長(zhǎng)今妄,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任鸳碧,我火速辦了婚禮盾鳞,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘杆兵。我一直安慰自己雁仲,他們只是感情好仔夺,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布琐脏。 她就那樣靜靜地躺著,像睡著了一般缸兔。 火紅的嫁衣襯著肌膚如雪日裙。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 52,441評(píng)論 1 310
  • 那天惰蜜,我揣著相機(jī)與錄音昂拂,去河邊找鬼。 笑死抛猖,一個(gè)胖子當(dāng)著我的面吹牛格侯,可吹牛的內(nèi)容都是我干的鼻听。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼联四,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼撑碴!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起朝墩,我...
    開(kāi)封第一講書人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤醉拓,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后收苏,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體亿卤,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年鹿霸,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了排吴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡懦鼠,死狀恐怖傍念,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情葛闷,我是刑警寧澤憋槐,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站淑趾,受9級(jí)特大地震影響阳仔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜扣泊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一近范、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧延蟹,春花似錦评矩、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至沥匈,卻和暖如春蔗喂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背高帖。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工缰儿, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人散址。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓乖阵,卻偏偏與公主長(zhǎng)得像宣赔,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子瞪浸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)拉背、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,121評(píng)論 4 61
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一種新的協(xié)議默终。它實(shí)...
    香橙柚子閱讀 23,909評(píng)論 8 183
  • 〇齐蔽、前言 與2016年制定年度計(jì)劃不同两疚,2017年和永澄老師制定年度計(jì)劃,只需要制定自己的2017年最想達(dá)成的三個(gè)...
    Doris壹燕閱讀 382評(píng)論 0 0
  • 2017已經(jīng)過(guò)去一半了含滴,盤點(diǎn)一下年初的計(jì)劃诱渤,實(shí)現(xiàn)的卻了了無(wú)幾√缚觯看到《驅(qū)動(dòng)力》的內(nèi)容后才發(fā)覺(jué)自己的沒(méi)找準(zhǔn)姿勢(shì)勺美。為什么...
    田田教練閱讀 143評(píng)論 0 2
  • 魅力提升之穿衣 之前講到了魅力提升中的工作部分,卓越的工作能力碑韵,在嚴(yán)格的自我管控制下以及專業(yè)知識(shí)的普及與積累是提高...
    錦瀟閱讀 216評(píng)論 0 2