質問

In a UITableViewController, this function logs into the console the x coordinate of the tableview cells

- (void)tableView:(UITableView *)tableView 
  willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1. Setup the CATransform3D structure
    CATransform3D rotation;
    rotation = CATransform3DMakeRotation( (45.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);

    //4. Define the final state (After the animation) and commit the animation
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:0.3];
    cell.layer.transform = CATransform3DIdentity;
    cell.alpha = 1;
    cell.layer.shadowOffset = CGSizeMake(0, 0);
    [UIView commitAnimations];
    NSLog(@"X%d= %d,Y%d=%d",(int)indexPath.row ,(int)cell.layer.position.x,(int)indexPath.row,(int)cell.layer.position.y);
}

it logs for the cells that are initially displayed (cell 0 to 12)

X0= 160,Y0=25
X1= 160,Y1=75
X2= 160,Y2=125
X3= 160,Y3=175
X4= 160,Y4=225
X5= 160,Y5=275
X6= 160,Y6=325
X7= 160,Y7=375
X8= 160,Y8=425
X9= 160,Y9=475
X10= 160,Y10=525
X11= 160,Y11=575
X12= 160,Y12=625

But after scrolling the tableview, it logs

X13= 0,Y13=675
X14= 0,Y14=725
X15= 0,Y15=775
X16= 0,Y16=825

and only cell 13's view is offset by 160 in the simulator

This could be fixed by adding to the previous method

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

What I am asking is why initially the x coordinate = 160 ???

役に立ちましたか?

解決

The position of a layer denotes its center position, and not the top-left corner. you changed that behaviour through manipulation of the anchorPoint property. Insert the following line after the line that contains anchorPoint:

cell.layer.position = CGPointMake(0, cell.layer.position.y);

Changing the anchorPoint also changes the frame. To keep the frame the same, you have to do the opposite movement with the position property.

(To get the position of the top-left corner, you would use cell.frame.origin instead.)

See also: CALayer reference

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top