美文网首页
iOS评论列表顶部渐隐

iOS评论列表顶部渐隐

作者: 暖羊羊_d603 | 来源:发表于2023-12-13 15:41 被阅读0次

仿抖音评论列表滑动,顶部渐隐效果

@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

相关文章

网友评论

      本文标题:iOS评论列表顶部渐隐

      本文链接:https://www.haomeiwen.com/subject/kvnbgdtx.html