iOS 多ScrollView滚动问题

2018-09-07  本文已影响71人  王律
#import <UIKit/UIKit.h>
@interface TableView : UITableView
@end

#import "TableView.h"

@interface TableView()<UIGestureRecognizerDelegate>

@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 ()<UITableViewDelegate,UITableViewDataSource>
@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
上一篇下一篇

猜你喜欢

热点阅读