// 根据状态做事情 if (state == MJRefreshStateIdle) { if (oldState != MJRefreshStateRefreshing) return; // 保存刷新时间 [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:self.lastUpdatedTimeKey]; [[NSUserDefaults standardUserDefaults] synchronize]; // 恢复inset和offset [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{ self.scrollView.mj_insetT += self.insetTDelta; // 自动调整透明度 if (self.isAutomaticallyChangeAlpha) self.alpha = 0.0; } completion:^(BOOL finished) { self.pullingPercent = 0.0; }]; }
不使用动画又分为两种情况:代码如下 第一种:
// 增加滚动区域 CGFloat top = self.scrollViewOriginalInset.top + self.zy_h; NSLog(@"scrollViewOriginalInset %f", self.scrollViewOriginalInset.top); NSLog(@"self.zy_h %f", self.zy_h); NSLog(@"top %f", top); // CGPoint offset = self.scrollView.contentOffset; UIEdgeInsets inset = self.scrollView.contentInset; inset.top = top; self.scrollView.contentInset = inset; // self.scrollView.contentInset {154, 0, 0, 0} NSLog(@"self.scrollView.contentInset %@", NSStringFromUIEdgeInsets(self.scrollView.contentInset)); CGPoint offset = self.scrollView.contentOffset; offset.y = - top; NSLog(@"offset %f", offset.y); self.scrollView.contentOffset = offset; // self.scrollView.contentOffset {0, -257.66666666666669} NSLog(@"self.scrollView.contentOffset %@", NSStringFromCGPoint(self.scrollView.contentOffset));
另一种正确的做法为:
// 增加滚动区域 CGFloat top = self.scrollViewOriginalInset.top + self.zy_h; NSLog(@"scrollViewOriginalInset %f", self.scrollViewOriginalInset.top); NSLog(@"self.zy_h %f", self.zy_h); NSLog(@"top %f", top); CGPoint offset = self.scrollView.contentOffset; UIEdgeInsets inset = self.scrollView.contentInset; inset.top = top; self.scrollView.contentInset = inset; // self.scrollView.contentInset {154, 0, 0, 0} NSLog(@"self.scrollView.contentInset %@", NSStringFromUIEdgeInsets(self.scrollView.contentInset)); // CGPoint offset = self.scrollView.contentOffset; // offset.y = - top; // // NSLog(@"offset %f", offset.y); self.scrollView.contentOffset = offset; // self.scrollView.contentOffset {0, -257.66666666666669} NSLog(@"self.scrollView.contentOffset %@", NSStringFromCGPoint(self.scrollView.contentOffset));