為tableView新增動畫

weixin_34138377發表於2016-07-06
1632693-0e2c9e22eb5a55b4.jpg
14425B30a2I0-NB5.jpg

在cell將要展示的時候做一個動畫,只需要把這段程式碼放在tablew的代理方法一起就OK了

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *array =  tableView.indexPathsForVisibleRows;
NSIndexPath *firstIndexPath = array[0];


//設定anchorPoint
cell.layer.anchorPoint = CGPointMake(0, 0.5);
//為了防止cell檢視移動,重新把cell放回原來的位置
cell.layer.position = CGPointMake(0, cell.layer.position.y);


//設定cell 按照z軸旋轉90度,注意是弧度
if (firstIndexPath.row < indexPath.row) {
    cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1.0);
}else{
    cell.layer.transform = CATransform3DMakeRotation(- M_PI_2, 0, 0, 1.0);
}


cell.alpha = 0.0;


[UIView animateWithDuration:1 animations:^{
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1.0;
}];
}

這是另外一種動畫效果

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
// 1. 配置CATransform3D的內容
CATransform3D transform;
transform = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
transform.m34 = 1.0/ -600;

// 2. 定義cell的初始狀態
cell.layer.shadowColor = [[UIColor blackColor]CGColor];
cell.layer.shadowOffset = CGSizeMake(10, 10);
cell.alpha = 0;

cell.layer.transform = transform;
cell.layer.anchorPoint = CGPointMake(0, 0.5);

// 3. 定義cell的最終狀態,並提交動畫
[UIView beginAnimations:@"transform" context:NULL];
[UIView setAnimationDuration:0.5];
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1;
cell.layer.shadowOffset = CGSizeMake(0, 0);
cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
[UIView commitAnimations];
}

相關文章