cell视觉差(二)

69 阅读1分钟
#import "ViewController.h"
#import "CustomScrollCell.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *imageNameArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageNameArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 14; i++) {
        [self.imageNameArray addObject:[NSString stringWithFormat:@"image%03ld.jpg",i]];
    }
    
    self.navigationController.navigationBar.translucent = NO;
    [self.view addSubview:self.tableView];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0.0;
        _tableView.sectionHeaderHeight = 0.0;
        _tableView.sectionFooterHeight = 0.0;
        _tableView.estimatedSectionHeaderHeight = 0.0;
        _tableView.estimatedSectionFooterHeight = 0.0;
        [_tableView registerClass:[CustomScrollCell class] forCellReuseIdentifier:NSStringFromClass([CustomScrollCell class])];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.imageNameArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [CustomScrollCell getHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomScrollCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomScrollCell class]) forIndexPath:indexPath];
    [cell resetParallaxState];
    cell.headerImageView.image = [UIImage imageNamed:[self.imageNameArray objectAtIndex:indexPath.row]];
    [cell parallaxWithView:cell.headerImageView offsetUp:60 offsetDown:50];
    //可以添加多个子视图进行视差化滚动,当就这个demo只让图片视差化滚动效果更佳
    //[cell parallaxWithView:cell.nameLabel offsetUp:10 offsetDown:10];
    [cell updateViewFrameWithScrollView:tableView];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    for (CustomScrollCell *cell in self.tableView.visibleCells) {
        [cell updateViewFrameWithScrollView:scrollView];
    }
}

@end