2015-11-20
很喜欢在安静的状态,
听着音乐,敲着键盘,
和代码们浓情对话,
每一份代码的积累,
都让自己觉得很充实快乐!Y(^_^)Y。
看到58同城app的cell有动画移动出现的特效,很是羡慕,但一直没有想到怎么去实现,今夜看了国外Yalantis团队的一个特效动画开源项目,获得了启发,于是根据自己想要的效果反复调试,终于也做出来了,觉得很开心(^_^)。
刚开始的思路是 通过屏幕可见row数组中去寻找和当前cell的indexPath索引值相符的进行判定,用了[[tableView indexPathsForVisibleRows] indexOfObject:indexPath]的方法, 可是在屏幕滑动太快的情况下会出现bug
//用这个方法来判定不太科学
if([[tableView indexPathsForVisibleRows] indexOfObject:indexPath] == NSNotFound)
{
//如果数据存在,返回指定的下标;
//但如果屏幕滑动太快会导致返回下标不存在,则返回NSNotFund,是一串字符“9223372036854775807”
NSLog(@"对象不在数组中===%lu ",[[tableView indexPathsForVisibleRows] indexOfObject:indexPath]);
}
接着通过获得可见row数组中的最后一个索引值NSIndexPath *lastIndexPath =[[tableView indexPathsForVisibleRows] lastObject]; 和当前cell的indexPath索引去对应 if (lastIndexPath == indexPath) ,让最后一行cell的出现时展现动画效果
接着就是个人喜好问题上的各种修修补补 (☆_☆)
1、想要屏幕一开始展现时屏幕上能看到的cell也有动画,于是参考Yalantis团队的一个特效动画开源项目代码的思路,在抽方法时进行优化,加了一个type选择判定
2、觉得屏幕下滑时顶部已经展示过的的cell也出现动画太闪眼了, 于是再加上判定
3、当然,还有奇奇怪怪的特殊情况,也给填补了
4、自己调试并且整理了一些动画的效果,以备自己后期项目所用,嘻嘻
//3D旋转动画
// CATransform3D rotation;
// rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
// rotation.m34 = 1.0/ -600;
//移动动画
/*CATransform3DMakeTranslation
(参数: tx::x平移 ty:y平移 tz:z平移)
从左向右移动(-100, 0, 0)[58同城cell移动效果]
从下向上移动(0, 200, 0) [我自己还是比较喜欢这种效果]
从右下向左上移动(100, 100, 0)
从上往下掉落下来(0, -200, 0)
*/
代码:
#pragma mark=====tableview delegate===== /** * cell 3D动画 */ - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ NSIndexPath *lastIndexPath =[[tableView indexPathsForVisibleRows] lastObject]; NSLog(@"indexPathsForVisibleRows===%lu====%lu===%ld===%ld",[[tableView indexPathsForVisibleRows] indexOfObject:indexPath], [[tableView indexPathsForVisibleRows]count], (long)indexPath.row, (long)lastIndexPath.row); //最后一行Cell才出现动画 if (lastIndexPath == indexPath) { [self animateVisibleCells:cell index:indexPath.row type:0]; //上滑时 为 0 不出现动画 }else if ([[tableView indexPathsForVisibleRows] indexOfObject:indexPath] == 0 ){ //第一次出现屏幕的cell 按照row数 依次 动画 type 1 }else if (indexPath.row < [[tableView indexPathsForVisibleRows]count] ){ [self animateVisibleCells:cell index:indexPath.row type:1]; //[特殊处理]如果屏幕滑动太快,返回不存在对象时,cell也出现动画 }else{ [self animateVisibleCells:cell index:indexPath.row type:0]; } //用这个方法来判定不太科学 if([[tableView indexPathsForVisibleRows] indexOfObject:indexPath] == NSNotFound) { //如果数据存在,返回指定的下标; //但如果屏幕滑动太快会导致返回下标不存在,则返回NSNotFund,是一串字符“9223372036854775807” NSLog(@"对象不在数组中===%lu ",[[tableView indexPathsForVisibleRows] indexOfObject:indexPath]); } }
/**
* cell动画
*
* @param cell cell
* @param index cell的row
* @param type type1 按照 row数 延迟展现 动画/ type2不延迟
*/
-(void)animateVisibleCells:(UITableViewCell * )cell index:(NSInteger)index type:(int)type{
CATransform3D translate = CATransform3DMakeTranslation(-100, 0, 0);[58同城cell移动效果]
cell.layer.shadowColor = [[UIColor blackColor]CGColor];
cell.layer.shadowOffset = CGSizeMake(10, 10);
cell.alpha = 0.0f;
cell.layer.transform = translate;
[UIView beginAnimations:@"translate" context:NULL];
[UIView setAnimationDuration:0.5f];
//type 1 按照 row数 延迟展现 动画
if (type == 1) {
[UIView setAnimationDelay:index * 0.1];
}
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1.0f;
cell.layer.shadowOffset = CGSizeMake(0, 0);
[UIView commitAnimations];
//旋转动画
// CATransform3D rotation;
// rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
// rotation.m34 = 1.0/ -600;
/*CATransform3DMakeTranslation
(参数: tx::x平移 ty:y平移 tz:z平移)
从左向右移动(-100, 0, 0)[58同城cell移动效果]
从下向上移动(0, 200, 0) [我自己还是比较喜欢这种效果]
从右下向左上移动(100, 100, 0)
从上往下掉落下来(0, -200, 0)
*/
}