上代碼穴豫。
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"跟著學(xué)習(xí)";
self.view.backgroundColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = YES;
/**
* [UIViewController setEdgesForExtendedLayout:]滓彰,它的默認(rèn)值為UIRectEdgeAll艳汽。當(dāng)你的容器是navigation controller時(shí)壶愤,默認(rèn)的布局將從navigation bar的頂部開(kāi)始。這就是為什么所有的UI元素都往上漂移了44pt乔夯。
*/
self.edgesForExtendedLayout = UIRectEdgeNone;
CGFloat w = [UIScreen mainScreen].bounds.size.width;
self.headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, 80)];
self.headerView.backgroundColor = [UIColor redColor];
self.headerView.textAlignment = NSTextAlignmentCenter;
self.headerView.text = @"我要隨著滾動(dòng)而慢慢消失或者出現(xiàn)-headerView";
self.headerView.textColor = [UIColor whiteColor];
[self.view addSubview:self.headerView];
self.stickyView = [[UILabel alloc] initWithFrame:CGRectMake(0, self.headerView.bottomY, w, 50)];
self.stickyView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.stickyView];
self.stickyView.textColor = [UIColor whiteColor];
self.stickyView.textAlignment = NSTextAlignmentCenter;
self.stickyView.text = @"滾動(dòng)到一定位置就固定-stickyView";
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.stickyView.bottomY, w, self.view.height - self.stickyView.bottomY)];
[self.view addSubview:self.tableView];
self.tableView.dataSource = self;
self.tableView.delegate = self;
/**
* decelerationRate的屬性來(lái)設(shè)置砖织,它的值域是(0.0原朝,1.0)
當(dāng)decelerationRate設(shè)置為0.1時(shí),當(dāng)手指touch up時(shí)就會(huì)很慢的停下來(lái)镶苞。
*/
self.tableView.decelerationRate = 0.1;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 保證是我們的tableivew
if (scrollView == self.tableView) {
// 保證我們是垂直方向滾動(dòng)喳坠,而不是水平滾動(dòng)
if (scrollView.contentOffset.x == 0) {
CGFloat y = scrollView.contentOffset.y;
/*!
* 往上是(+) 往下為(-)
*/
// NSLog(@"Y:%.2f",y);
// 這個(gè)是非常關(guān)鍵的變量,用于記錄上一次滾動(dòng)到哪個(gè)偏移位置
static CGFloat previousOffsetY = 0;
// 向上滾動(dòng)
if (y > 0) {
/*!
* self.headerView.bottomY 開(kāi)始為80 --> 0
*/
// NSLog(@"Y:%.2f",self.headerView.bottomY);
if (self.headerView.bottomY <= 0) {
return;
}
// NSLog(@"Y:%.2f",fabs(y - previousOffsetY));
// 計(jì)算兩次回調(diào)的滾動(dòng)差:fabs(y - previousOffsetY)值
/*!
* 上推
* 1.假如第一次:y直接到了80 之前的為0茂蚓,bottomY = 80-(80-0) = 0;
* 2.假如第一次:y直接到了40 之前的為0壕鹉,bottomY = 80-(40-0) = 40
* 第二次:y直接到了80 之前的為40,bottomY = 40-(80-40)= 0
* 下滑
* 1.假如第一次:y直接到了0 之前的為0聋涨,bottomY = 0-(0-0) = 0;
* 2.假如第一次:y直接到了40 之前的為0晾浴,bottomY = 80-(40-0) = 40
* 第二次:y直接到了80 之前的為40,bottomY = 40-(80-40)= 0
*
*/
CGFloat bottomY = self.headerView.bottomY - fabs(y - previousOffsetY);//fabs:取float類(lèi)型的絕對(duì)值
bottomY = bottomY >= 0 ? bottomY : 0;
self.headerView.bottomY = bottomY;
self.stickyView.y = self.headerView.bottomY;
self.tableView.frame = CGRectMake(0, self.stickyView.bottomY,
scrollView.width,
self.view.height - self.stickyView.bottomY);
previousOffsetY = y;
// NSLog(@"Y:%.2f",fabs(previousOffsetY));
// 如果一直不松手滑動(dòng)牍白,重復(fù)向上向下滑動(dòng)時(shí)脊凰,如果沒(méi)有設(shè)置還原為0,則會(huì)出現(xiàn)馬上到頂?shù)那闆r茂腥。
/*!
* 因?yàn)楫?dāng)往下回的時(shí)候狸涌,y = 0;
* 如果 previousOffsetY 不置為0 最岗,fabs(y - previousOffsetY) = (0-80) = 80帕胆,最終的bottomY立馬就等于0,立馬就固定死了
* 清空之后般渡,
* 往下拉懒豹,因?yàn)閥==0,所以不會(huì)走到這里驯用,上面就直接return了
* 往上推脸秽,又開(kāi)始了,
*/
if (previousOffsetY >= self.headerView.height) {
previousOffsetY = 0;
}
}
// 向下滾動(dòng)
else if (y < 0) {
// NSLog(@"Y:%.2f",self.headerView.y);
NSLog(@"Y:%.2f",y);
if (self.headerView.y >= 0) {
return;
}
/*!
* fabs(y) = 0--無(wú)窮大
*/
CGFloat bottomY = self.headerView.bottomY + fabs(y);
bottomY = bottomY <= self.headerView.height ? bottomY : self.headerView.height;
self.headerView.bottomY = bottomY;
self.stickyView.y = self.headerView.bottomY;
self.tableView.frame = CGRectMake(0,
self.stickyView.bottomY,
scrollView.width,
self.view.height - self.stickyView.bottomY);
}
}
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = @"這只是展示固定數(shù)據(jù)蝴乔,沒(méi)有別的作用记餐。";
return cell;
}