iOS Coding项目片段记录(三)

本文介绍了一种实现UITableView固定section随cell滚动的方法。通过重写scrollViewDidScroll方法,调整scrollView的内容偏移量,使section始终处于屏幕顶部,提供更好的用户体验。

UITableView 固定section 随着cell滚动而滚动

实现UITableView 的下面这个方法,
#pragma mark - Scroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat sectionHeaderHeight = 40;
       //固定section 随着cell滚动而滚动
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }

}

实际项目中实现:

/frame   自定义head
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        
        if ([self.delegate respondsToSelector:@selector(tableViewDidScroll:)]) {
            [self.delegate tableViewDidScroll:scrollView];
        }
        
    });
    
    
    if (isAnCompletion) {
        return;
    }
    [self.listTableView updateTimeLabel];
    if (lastContentOffset < scrollView.contentOffset.y) {
        //向上滚动
        //head
        //如果导航在中间
        if (self.fsofViewController.navView.frame.origin.y > 64) {
//            self.fsofViewController.navView.hidden = YES;
            
            [UIView animateWithDuration:0.5 animations:^{
                isAnCompletion = YES;
                
                self.fsofViewController.navView.alpha = 0.3;
                self.fsofViewController.navView.alpha = -self.fsofViewController.headView.frame.origin.y/(self.fsofViewController.headView.frame.size.height-self.listuserNavbarHeight);
//                self.fsofViewController.headView.alpha =0;
                //导航条透明度
                self.fsofViewController.barBgView.alpha = -self.fsofViewController.headView.frame.origin.y/(self.fsofViewController.headView.frame.size.height-self.listuserNavbarHeight);
                
                self.fsofViewController.headView.frame = CGRectMake(0, -self.listuserHeadHeight+self.listuserNavbarHeight, self.fsofViewController.headView.frame.size.width, self.fsofViewController.headView.frame.size.height);
                //导航
                self.fsofViewController.navView.frame = CGRectMake(0, self.listuserNavbarHeight, self.fsofViewController.navView.frame.size.width, self.fsofViewController.navView.frame.size.height);
                
                self.fsofViewController.headView.alpha =0.5;
                //所有的list一起变
                for (id view in self.fsofViewController.bgScrollView.subviews) {
                    if ([view isKindOfClass:[CommunityList class]]) {
                        CommunityList *cView;
                        cView = view;
                        cView.frame = CGRectMake(cView.frame.origin.x, self.listuserNavbarHeight, self.frame.size.width, [UIScreen mainScreen].bounds.size.height-self.listtabbarHeight-self.listuserNavbarHeight);
                        cView.listTableView.frame = CGRectMake(0, 0, self.frame.size.width, [UIScreen mainScreen].bounds.size.height-self.listtabbarHeight-self.listuserNavbarHeight);
                    }
                }
                
            } completion:^(BOOL finished) {
                isAnCompletion = NO;
                self.fsofViewController.headView.alpha =0;
//                self.fsofViewController.navView.alpha = 1;
            }];
            
        }
        
    }else{
        
        if (scrollView.contentOffset.y <= -1) {
            
            //向下滚动
            [UIView animateWithDuration:0.5 animations:^{
                isAnCompletion = YES;
                self.fsofViewController.navView.alpha =1;
                self.fsofViewController.headView.alpha =1;
                self.fsofViewController.barBgView.alpha =0;
                
                self.fsofViewController.headView.frame = CGRectMake(0, 0, self.fsofViewController.headView.frame.size.width, self.fsofViewController.headView.frame.size.height);
                //导航
                self.fsofViewController.navView.frame = CGRectMake(0, self.listuserHeadHeight, self.fsofViewController.navView.frame.size.width, self.fsofViewController.navView.frame.size.height);
                
                
                //所有的list一起变
                for (id view in self.fsofViewController.bgScrollView.subviews) {
                    if ([view isKindOfClass:[CommunityList class]]) {
                        
                        CommunityList *cView;
                        cView = view;
                        
                        cView.frame = CGRectMake(cView.frame.origin.x, self.listuserHeadHeight+self.listheadHeight, self.frame.size.width, [UIScreen mainScreen].bounds.size.height-self.listuserHeadHeight-self.listheadHeight-self.listtabbarHeight);
                        
                        cView.listTableView.frame = CGRectMake(0, 0, self.frame.size.width, [UIScreen mainScreen].bounds.size.height-self.listuserHeadHeight-self.listtabbarHeight-self.listheadHeight);
                    }
                }
                
                
            } completion:^(BOOL finished) {
                isAnCompletion = NO;
            }];
        
            
            
        }
    }
    
    if(self.fsofViewController.navView.frame.origin.y == 64){
        //导航条透明度
        [UIView animateWithDuration:0.5 animations:^{
            self.fsofViewController.barBgView.alpha = 1.0;
        }];
    }else{
        
        //导航条透明度
        self.fsofViewController.barBgView.alpha = 0.0;
    }
}

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

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

 

转载于:https://www.cnblogs.com/741162830qq/p/6030838.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值