当cell将要显示时调用
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
函数,可以在这里进行Cell内子控件属性的修改,完成所需要的动画效果。
代码:
@property (assign, nonatomic) CGFloat lastScrollOffset;
@property (assign, nonatomic) CGFloat move;
- (void)viewDidLoad {
[super viewDidLoad];
_move = [UIScreen mainScreen].bounds.size.width;
_lastScrollOffset = 0;
}
下拉和上拖动画加载的方向不同,先判断手势的的方向
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView != self.tableview) return;
CGFloat y = scrollView.contentOffset.y;
if (y > _lastScrollOffset) {//用户往上拖动
_move = [UIScreen mainScreen].bounds.size.width;
} else {//用户往下拖动
_move = -[UIScreen mainScreen].bounds.size.width;
}
//存储最后的y值
_lastScrollOffset = y;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger baseRows = ceilf(CGRectGetHeight(self.tableview.bounds) / 100) - 1;
CGFloat delay = indexPath.row <= baseRows ? 0.05f * indexPath.row : 0.01f;
cell.alpha = 0.0;
cell.transform = CGAffineTransformIdentity;
CGFloat xMove = _move;
cell.transform = CGAffineTransformMakeTranslation(xMove, 0);
[UIView animateWithDuration:1.0f delay:delay usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
cell.alpha = 1.0;
cell.transform = CGAffineTransformMakeTranslation(0, 0);
} completion:^(BOOL finished) {
}];
}
NSInteger baseRows = ceilf(CGRectGetHeight(self.tableview.bounds) / 100) - 1;中的100是cell的高度。