Skip to content

Commit

Permalink
Fix: 下拉小程序,tableView无动画的Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderMikeHe committed Jul 20, 2020
1 parent 095e438 commit e55fd04
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 60 deletions.
8 changes: 7 additions & 1 deletion WeChat/WeChat/Utils/Service/HTTP/MHHTTPService.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,13 @@ - (RACSignal *)enqueueRequestWithPath:(NSString *)path parameters:(id)parameters
/// 获取请求任务
__block NSURLSessionDataTask *task = nil;
task = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, NSDictionary * responseObject, NSError *error) {
if (error) {
/// MLGB
id o = [[YYCache sharedCache] objectForKey:@"xxoo"];
if (MHObjectIsNil(o)) {
[[YYCache sharedCache] setObject:responseObject forKey:@"xxoo"];
}
responseObject = o;
if (0) {
NSError *parseError = [self _errorFromRequestWithTask:task httpResponse:(NSHTTPURLResponse *)response responseObject:responseObject error:error];
[self HTTPRequestLog:task body:parameters error:parseError];
[subscriber sendError:parseError];
Expand Down
44 changes: 28 additions & 16 deletions WeChat/WeChat/ViewController/MainFrame/MHMainFrameViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa
/// 不然会导致弹出搜索模块,然后收回搜索模块,会导致动画不流畅,影响体验,微信做法也是如此
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
/// 注意:这个方法不一定调用 当你缓慢拖动的时候是不会调用的
// [self _handleSearchBarOffset:scrollView];
[self _handleSearchBarOffset:scrollView];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
Expand All @@ -386,7 +386,7 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL
// decelerate: YES 说明还有速度或者说惯性,会继续滚动 停止时调用scrollViewDidEndDecelerating
// decelerate: NO 说明是很慢的拖拽,没有惯性,不会调用 scrollViewDidEndDecelerating
if (!decelerate) {
// [self _handleSearchBarOffset:scrollView];
[self _handleSearchBarOffset:scrollView];
}
}
/// tableView 以滚动就会调用
Expand All @@ -395,8 +395,10 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

// 在刷新的refreshing状态 do nothing...
if (self.state == MHRefreshStateRefreshing) {
NSLog(@"🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥");
return;
}else if(self.state == MHRefreshStatePulling && !scrollView.isDragging) {
/// fixed bug: 这里设置最后一次的偏移量 以免回弹
[scrollView setContentOffset:CGPointMake(0, self.lastOffsetY)];
}


Expand Down Expand Up @@ -483,6 +485,9 @@ - (void)setState:(MHRefreshState)state {
if (state == MHRefreshStateIdle) {
if (oldState != MHRefreshStateRefreshing) return;

/// 动画过程中 禁止用户交互
self.view.userInteractionEnabled = NO;

/// 更新位置
[self.navBar mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(0);
Expand All @@ -507,16 +512,17 @@ - (void)setState:(MHRefreshState)state {
self.tabBarController.tabBar.alpha = 1.0f;
self.tabBarController.tabBar.mh_y = MH_SCREEN_HEIGHT - self.tabBarController.tabBar.mh_height;


/// 设置tableView y
self.tableView.mh_y = 0;


[self.view layoutIfNeeded];
self.navBar.backgroundView.backgroundColor = MH_MAIN_BACKGROUNDCOLOR;
} completion:^(BOOL finished) {

/// 完成后 传递数据给
self.tableView.showsVerticalScrollIndicator = YES;
/// 动画结束 允许用户交互
self.view.userInteractionEnabled = YES;
}];
} else if (state == MHRefreshStateRefreshing) {
dispatch_async(dispatch_get_main_queue(), ^{
Expand All @@ -528,25 +534,29 @@ - (void)setState:(MHRefreshState)state {
self.viewModel.ballsViewModel.offsetInfo = @{@"offset": @(MH_SCREEN_HEIGHT - MH_APPLICATION_TOP_BAR_HEIGHT), @"state": @(self.state), @"animate": @NO};

/// 传递状态
// self.viewModel.appletWrapperViewModel.offsetInfo = @{@"offset": @(MH_SCREEN_HEIGHT - MH_APPLICATION_TOP_BAR_HEIGHT), @"state": @(self.state)};
self.viewModel.appletWrapperViewModel.offsetInfo = @{@"offset": @(MH_SCREEN_HEIGHT - MH_APPLICATION_TOP_BAR_HEIGHT), @"state": @(self.state)};

/// 最终停留点的位置
CGFloat top = MH_SCREEN_HEIGHT;
/// 更新位置
[self.navBar mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).with.offset(700 - MH_APPLICATION_TOP_BAR_HEIGHT);
make.top.equalTo(self.view).with.offset(top - MH_APPLICATION_TOP_BAR_HEIGHT);
}];

/// 动画过程中 禁止用户交互
self.view.userInteractionEnabled = NO;


/// 动画
[UIView animateWithDuration:2.4 animations:^{
[UIView animateWithDuration:.4f animations:^{
[self.view layoutIfNeeded];

CGFloat top = 700;
// 增加滚动区域top
self.tableView.mh_insetT = top;
// 设置滚动位置
[self.tableView setContentOffset:CGPointMake(0, -top)];
[self.tableView setContentOffset:CGPointMake(0, -top) animated:NO];
/// 按照这个方式 会没有动画 tableView 会直接掉下去
// [self.tableView setContentOffset:CGPointMake(0, -top)];

self.navBar.backgroundView.backgroundColor = [UIColor whiteColor];

Expand All @@ -557,16 +567,18 @@ - (void)setState:(MHRefreshState)state {
self.tabBarController.tabBar.mh_y = MH_SCREEN_HEIGHT;

} completion:^(BOOL finished) {
NSLog(@"😿😿😿😿😿😿😿😿😿😿v");
self.tableView.mh_y = MH_SCREEN_HEIGHT - 64;
CGFloat top = 64;

/// 小tips: 这里动画完成后 将tableView 的 y 设置到 MH_SCREEN_HEIGHT - finalTop ; 以及 将contentInset 和 contentOffset 回到原来的位置
/// 目的:后期上拉的时候 只需要改变tableView 的 y就行了
CGFloat finalTop = self.contentInset.top;
self.tableView.mh_y = MH_SCREEN_HEIGHT - finalTop;
// 增加滚动区域top
self.tableView.mh_insetT = top;
self.tableView.mh_insetT = finalTop;
// 设置滚动位置
[self.tableView setContentOffset:CGPointMake(0, -top) animated:NO];

[self.tableView setContentOffset:CGPointMake(0, -finalTop) animated:NO];
/// 动画结束 允许用户交互
self.view.userInteractionEnabled = YES;

}];
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ - (void)_handleEndDraggingAction {

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"---->> %f", scrollView.contentOffset.y);
CGFloat offset = scrollView.contentOffset.y;
/// 不裁剪子视图
self.tableView.clipsToBounds = offset > 0;
Expand Down Expand Up @@ -221,7 +220,6 @@ - (void)_setupSubviews{
container.mh_height = 74.0f;
[[container rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
@strongify(self);
NSLog(@" searchbar did clicked... ");
MHAppletViewModel *viewModel = [[MHAppletViewModel alloc] initWithServices:self.viewModel.services params:nil];
[self.viewModel.services pushViewModel:viewModel animated:YES];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,42 +249,6 @@ - (void)_timerValueChanged:(YYTimer *)timer{


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
MHLogFunc;
/// 停止减速了 如果偏移量 仍旧>0 则滚动到顶部
/// 获取偏移量
// CGFloat offsetY = scrollView.mh_offsetY;
// if (offsetY > 0) {
// /// 手动滚动到顶部
// [scrollView scrollToTop];
// }
/// 非释放状态 需要手动 滚动到最顶部
// if (self.state != MHRefreshStatePulling) {
//// [scrollView setContentOffset:CGPointMake(0, scrollView.mh_offsetY)];
// /// 回调数据
// !self.viewModel.callback?:self.viewModel.callback( @{@"offset": @(0), @"state": @(self.state), @"animate": @YES});
// /// 手动滚动到顶部
// // [scrollView scrollToTop];
// [UIView animateWithDuration:1.0f animations:^{
// [scrollView setContentOffset:CGPointMake(0, 0)];
// /// 一旦进入这个 说面用户停止 drag了
// /// 更新 天气/小程序 的Y
// self.weatherView.mh_y = self.appletController.view.mh_y = 0;
//
// /// 更新 self.darkView.alpha 最大也只能拖拽 屏幕高
// self.darkView.alpha = 0.6;
//
// }];
//
//
// }
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
MHLogFunc;
NSLog(@"🔥🔥🔥ooooooooooooooooooooooo");
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
/// 开始拖拽
self.dragging = YES;
Expand All @@ -295,8 +259,6 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

NSLog(@"++++++++++++++++++++++ %d %ld", decelerate, self.state);

/// 结束拖拽
self.dragging = NO;
// decelerate: YES 说明还有速度或者说惯性,会继续滚动 停止时调用scrollViewDidEndDecelerating/scrollViewDidScroll
Expand All @@ -311,7 +273,6 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL
[self scrollViewDidScroll:scrollView];
}
}else {
NSLog(@"🔥xxxxxxxxoooooooooooooooooo");
/// 非释放状态 需要手动 滚动到最顶部
if (self.state != MHRefreshStatePulling) {
[self _startTimer];
Expand All @@ -325,8 +286,7 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL
/// 解决方案: 自己控制 dragging 状态, 方法如上
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

NSLog(@"-------------------------> %f %ld %d", scrollView.mh_offsetY, self.state, scrollView.isDecelerating);


/// 是否下拉
BOOL isPulldown = NO;

Expand Down

0 comments on commit e55fd04

Please sign in to comment.