iOS开发---UIscrollView嵌套tableview的悬停效果实现方式
一直以来,苹果的悬停效果,没有太多的逻辑,所以直接使用UITableview的组头悬停即可
但是最近的悬停效果比较繁琐,如果继续采用这方式,加上刷新的逻辑之后,或者有二级悬停之后,就不再好使了!
所以只能自己写这种效果了
遇到的坑,一开始以为只要判断悬停的位置,然后对两个控件进行 滚动属性的切换即可,但是发现有问题,到了临界点,有一下卡顿,滚动停止,父视图或者子视图,并不能完美的跟上滚动!所以这种方案就夭折了!
新方案:依然采用监测临界点的方法,但是这次通过设置两个视图的contenoffset属性,其实两个都在滚动,只是有一个一直在同一位置,视觉上就是一个在滚,一个等待了,到了临界点,滚动状态切换为相反,这样就实现了!
同时还有一个关键点就是要设置一下滚动视图的属性!下边直接上代码!
#import <UIKit/UIKit.h> @interface CourseTableView : UITableView<UIGestureRecognizerDelegate> @end
#import "CourseTableView.h"
@implementation CourseTableView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
@end
//该方法返回YES时,意味着所有相同类型的手势都会得到处理。
下面是简单的临界点判断逻辑
//父tableview @property(nonatomic,assign)BOOL canScroll; //子tableview @property (nonatomic, assign) BOOL sonCanScroll;
#pragma mark UIScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat bottom = [_tableView rectForSection:0].size.height+MainScreenWidth*140/375.0;
   
    
    if (scrollView == _tableView) {//父视图
        CGFloat scrollViewY = _tableView.contentOffset.y;
        if (scrollViewY>=bottom) {//当subcell还没有滚动到
            _tableView.contentOffset = CGPointMake(0, bottom);
            if (self.canScroll) {
                self.canScroll = NO;
                self.sonCanScroll = YES;
                NSLog(@"父视图悬停---111");
            }
        }else{
            if (!self.canScroll) {//子cell没到顶
                if (_courseTableview.contentOffset.y == 0) {
                    self.canScroll = YES;
                    self.sonCanScroll = NO;
                    NSLog(@"父视图动---2222");
                }else{
                    _tableView.contentOffset = CGPointMake(0, bottom);
                    NSLog(@"父视图悬停---2222");
                }
               
            }
        }
        
        if (_tableView.contentOffset.y == bottom &&_courseTableview.contentOffset.y == 0) {
            self.canScroll = YES;
        }
        
    }
    //    NSLog(@"%lf",scrollView.contentOffset.y);
    if (scrollView == _courseTableview) {//子视图
        CGFloat scrollViewY = _courseTableview.contentOffset.y;
        if (!self.sonCanScroll&&self.canScroll) {
            _courseTableview.contentOffset = CGPointZero;
            NSLog(@"子视图悬停---111");
        }
        if (scrollViewY <= 0) {
            self.sonCanScroll = NO;
            _courseTableview.contentOffset = CGPointZero;
            self.canScroll = YES;
            NSLog(@"子视图悬停---2222");
        }
        
        if (_tableView.contentOffset.y == bottom &&_courseTableview.contentOffset.y == 0) {
            self.canScroll = YES;
        }
        
    }
}
byzqk
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号