动画后UITableViewCell位置关闭

时间:2014-07-18 04:33:50

标签: ios objective-c uitableview core-animation

我在-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)进行动画制作。细胞基本上旋转(下面的代码)。他们每次加载到屏幕上时都会这样做。最初旋转的单元格(即开头屏幕上可见的单元格)看起来像"之前"快照如下。小(正常)缩进。向下滚动或向左滚动后,新单元格会在屏幕上显示动画,但它们看起来像" After"射击。它们被几个像素所取代。一旦我再次向上滚动,上部单元格将被重新加载,并且还有更大的缩进。所有这些都没问题,除了取决于滚动行为,有时一些单元格是缩进的,而其他单元格则不是,它看起来很糟糕。我尝试重置UITableViewCell的缩进属性,但它没有做任何事情。什么可能导致这种奇怪行为的想法?也许我不知道我应该设置的属性?谢谢!

在:

enter image description here

后:

enter image description here

//Animation Code Borrowed From http://www.thinkandbuild.it/animating-uitableview-cells/
//1. Setup the CATransform3D structure
CATransform3D rotation;
rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
rotation.m34 = 1.0/ -600;

//2. Define the initial state (Before the animation)
cell.layer.shadowColor = [[UIColor blackColor]CGColor];
cell.layer.shadowOffset = CGSizeMake(10, 10);
cell.alpha = 0;

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

//3. Define the final state (After the animation) and commit the animation
[UIView beginAnimations:@"rotation" context:NULL];
[UIView setAnimationDuration:0.8];
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1;
cell.layer.shadowOffset = CGSizeMake(0, 0);
[UIView commitAnimations];

修改 在再次访问原始网站的源代码后,我注意到代码的更新,我应用了它:

if(cell.layer.position.x != 0){
    cell.layer.position = CGPointMake(0, cell.layer.position.y);
}

然而,它似乎没有解决这个问题,我认为这是解决这个动画的另一个定位问题。

1 个答案:

答案 0 :(得分:2)

我不确定只是尝试一下,

只需将图层位置强制为原点,请参见下面的代码

//1. Setup the CATransform3D structure
CATransform3D rotation;
rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
rotation.m34 = 1.0/ -600;


//2. Define the initial state (Before the animation)
cell.layer.shadowColor = [[UIColor blackColor]CGColor];
cell.layer.shadowOffset = CGSizeMake(10, 10);
cell.alpha = 0;

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

if(cell.layer.position.x != 0)
{
    cell.layer.position = CGPointMake(0, cell.layer.position.y);
}

//add this and try
CGPoint point = cell.layer.position;
point.x = 0.0f; //setting the position back to original (for your case)
cell.layer.position = point;


//4. Define the final state (After the animation) and commit the animation
[UIView beginAnimations:@"rotation" context:NULL];
[UIView setAnimationDuration:0.8];
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1;
cell.layer.shadowOffset = CGSizeMake(0, 0);
[UIView commitAnimations];