Skip to content

Commit

Permalink
仿美团的demo增加正在刷新的监听
Browse files Browse the repository at this point in the history
正在刷新时不允许左右滑动scrollView切换
  • Loading branch information
SPStore authored and SPStore committed Aug 7, 2017
1 parent 05c467e commit 9a1a9e0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView == self.scrollView) {
NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
</Bucket>
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@
#import <UIKit/UIKit.h>
#import "MyHeaderView.h"

typedef NS_ENUM(NSInteger,RefreshingState) {
RefreshingStateRefreshing, // 正在刷新
RefreshingStateEndRefresh, // 结束刷新
};

@interface BaseViewController : UIViewController
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) MyHeaderView *headerView;
@property (nonatomic, assign) CGPoint lastContentOffset;

@property (nonatomic, assign) BOOL isFirstViewLoaded;

@property (nonatomic, assign) BOOL refreshState;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ - (void)viewDidLoad {
[self.view addSubview:self.tableView];
self.scrollView = self.tableView;


self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
// 下拉刷新
[self downPullUpdateData];
Expand All @@ -34,11 +35,17 @@ - (void)viewDidLoad {
}];
}


// 下拉刷新
- (void)downPullUpdateData {

[[NSNotificationCenter defaultCenter] postNotificationName:@"isRefreshing" object:@(RefreshingStateRefreshing)];

// 模拟网络请求,1秒后结束刷新
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.rowCount = 20;
[[NSNotificationCenter defaultCenter] postNotificationName:@"isRefreshing" object:@(RefreshingStateEndRefresh)];

[self.tableView.mj_header endRefreshing];
});
}
Expand All @@ -47,9 +54,13 @@ - (void)downPullUpdateData {
- (void)upPullLoadMoreData {
self.rowCount = 30;
[self.tableView reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName:@"isRefreshing" object:@(RefreshingStateRefreshing)];

// 模拟网络请求,1秒后结束刷新
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.rowCount = 20;
[[NSNotificationCenter defaultCenter] postNotificationName:@"isRefreshing" object:@(RefreshingStateEndRefresh)];

[self.tableView.mj_footer endRefreshing];
});
}
Expand All @@ -61,10 +72,6 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.lastContentOffset = scrollView.contentOffset;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self.lastContentOffset = scrollView.contentOffset;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.rowCount;
}
Expand Down
28 changes: 27 additions & 1 deletion HVScrollView(仿美团)/HVScrollView(仿美团)/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,38 @@ - (void)viewDidLoad {
// 先将第一个子控制的view添加到scrollView上去
[self.scrollView addSubview:self.childViewControllers[0].view];


// 监听子控制器发出的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subScrollViewDidScroll:) name:@"SubScrollViewDidScroll" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshing:) name:@"isRefreshing" object:nil];

}

#pragma mark - 刷新通知
- (void)refreshing:(NSNotification *)noti {
RefreshingState state = [noti.object integerValue];
if (state == RefreshingStateRefreshing) {
self.scrollView.userInteractionEnabled = NO;
} else {
self.scrollView.userInteractionEnabled = YES;
}
}

// self.scrollView的代理方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

BaseViewController *baseVc = self.childViewControllers[_selectedIndex];
if (scrollView == self.scrollView) {
// 如果scrollView的内容很少,在屏幕范围内,则自动回落
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (baseVc.scrollView.contentSize.height < kScreenH && [baseVc isViewLoaded]) {
[baseVc.scrollView setContentOffset:CGPointMake(0, -kScrollViewBeginTopInset) animated:YES];
}
});
}
}


// 子控制器上的scrollView已经滑动的代理方法所发出的通知方法(核心)
- (void)subScrollViewDidScroll:(NSNotification *)noti {

Expand Down

0 comments on commit 9a1a9e0

Please sign in to comment.