iOS評論列表頂部漸隱

仿抖音評論列表滑動,頂部漸隱效果

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *layerView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect frame = CGRectMake(100, 100, 200, 400);
    self.layerView.frame = frame;
    [self.view addSubview:self.layerView];
    
    self.tableView.frame = self.layerView.bounds;
    [self.layerView addSubview:self.tableView];
    
    self.gradientLayer.frame = self.layerView.bounds;
    [self.layerView.layer setMask:self.gradientLayer];
    
    // 通過設置layer.mask 來實現漸隱
    NSArray *colors = @[(id)[UIColor colorWithWhite:0 alpha:0].CGColor,
                        (id)[UIColor colorWithWhite:0 alpha:1.0].CGColor,
                    ];
    [self.gradientLayer setColors:colors];
    [_gradientLayer setStartPoint:CGPointMake(0, 0)];
    [_gradientLayer setEndPoint:CGPointMake(0, 0.1)];
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"UITableViewCell"];
    cell.textLabel.text = [NSString stringWithFormat:@"第 %d 行",indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

#pragma mark - getter
- (CAGradientLayer *)gradientLayer {
    if (!_gradientLayer) {
        _gradientLayer = [CAGradientLayer layer];
    }
    return _gradientLayer;
}

- (UIView *)layerView {
    if (!_layerView) {
        _layerView = [[UIView alloc] init];
    }
    return _layerView;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate  = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容