仿抖音评论列表滑动,顶部渐隐效果
@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