需求
最近項(xiàng)目有一個(gè)需求渗柿,涉及到在UIScrollView嵌套的使用个盆,且里面的UIScrollView實(shí)際上是分頁(yè)控制器。需求類似于下圖:
其實(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é)比較好用,如下圖:
這一部分可以直接使用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ì)你們有所幫助邪铲。