天天看點

如何給tableviewcell加3D旋轉動畫

常用的3D動畫類型同仿射變化一樣有旋轉平移縮放,如下:

 CATransform3DMakeScale(0.5, 0.5, 1.0);  //x,y,z放大縮小倍數

 CATransform3DMakeRotation(1.57, 1, 1, 0); //1.57表示所轉角度的弧度 = 90Pi/180 = 90*3.14/180

CATransform3DMakeTranslation(0, 0, 0); //位置移動

我們想給tableview在cell即将出現的時間添加動畫,用到的tableview代理方法如下:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

//添加每個cell出現時的3D動畫

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    CATransform3D rotation;//3D旋轉初始化對象

    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);//角度控制

    //逆時針旋轉

    rotation.m34 = 1.0/ -600;

    cell.layer.shadowColor = [[UIColor blackColor]CGColor];

    cell.layer.shadowOffset = CGSizeMake(10, 10);

    cell.alpha = 0;

    cell.layer.transform = rotation;

    [UIView beginAnimations:@"rotation"context:NULL];

    //旋轉時間

    [UIView setAnimationDuration:0.8];

    cell.layer.transform = CATransform3DIdentity;

    cell.alpha = 1;

    cell.layer.shadowOffset = CGSizeMake(0, 0);

    [UIView commitAnimations];

}

另外一種寫法稍有不同,UIView動畫改用block來進行簡化,

如何給tableviewcell加3D旋轉動畫

最後上一張效果圖:

如何給tableviewcell加3D旋轉動畫

繼續閱讀