iOS 多ScrollView滚动问题

#import 
@interface TableView : UITableView
@end

#import "TableView.h"

@interface TableView()

@end
@implementation TableView

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
@end
#import "ViewController.h"
#import "TableView.h"

#define SCREEN_WIDTH    ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT   ([UIScreen mainScreen].bounds.size.height)

#define TABLE_HEAD   200

@interface ViewController ()
@property (nonatomic,strong) UIScrollView *mainScrollView;
@property (nonatomic,strong) TableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.mainScrollView];
    [self.mainScrollView addSubview:self.tableView];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView==self.tableView) {
        if(self.mainScrollView.contentOffset.y < TABLE_HEAD){//head还没消失
            self.tableView.contentOffset = CGPointZero;
        }else{//head已经消失
            self.mainScrollView.contentOffset = CGPointMake(0, TABLE_HEAD);
        }
    }else{
        if (self.tableView.contentOffset.y>0) {
            self.mainScrollView.contentOffset = CGPointMake(0, TABLE_HEAD);
        }else{
            self.tableView.contentOffset = CGPointZero;
        }
    }
}

#pragma mark - tableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
    }
    cell.textLabel.text = [@(indexPath.row)stringValue];
    return cell;
}

- (UIScrollView *)mainScrollView
{
    if (_mainScrollView==nil) {
        _mainScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        _mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+TABLE_HEAD);
        _mainScrollView.backgroundColor = [UIColor redColor];
        _mainScrollView.delegate = self;
    }
    return _mainScrollView;
}

- (TableView *)tableView
{
    if (_tableView==nil) {
        _tableView = [[TableView alloc]initWithFrame:CGRectMake(0, TABLE_HEAD, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _tableView.backgroundColor = [UIColor greenColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
@end

你可能感兴趣的:(iOS 多ScrollView滚动问题)