views:

30

answers:

1

I am using the following to scale and reposition a UIView layer when the device rotates to landscape.

 [containerView.layer setValue:[NSNumber numberWithFloat: 0] forKeyPath: @"transform.translation.x"];
 [containerView.layer setValue:[NSNumber numberWithFloat: 0] forKeyPath: @"transform.translation.y"];    
 [containerView.layer setValue:[NSNumber numberWithFloat: 1] forKeyPath: @"transform.scale.x"]; //[NSNumber numberWithInt:1]
 [containerView.layer setValue:[NSNumber numberWithFloat: 1] forKeyPath: @"transform.scale.y"];

and then the folowing when rotating back to portrait

 [containerView.layer setValue:[NSNumber numberWithFloat: -75] forKeyPath: @"transform.translation.x"];
 [containerView.layer setValue:[NSNumber numberWithFloat: 0] forKeyPath: @"transform.translation.y"];    
 [containerView.layer setValue:[NSNumber numberWithFloat: .7] forKeyPath: @"transform.scale.x"]; //[NSNumber numberWithInt:1]
 [containerView.layer setValue:[NSNumber numberWithFloat: .7] forKeyPath: @"transform.scale.y"];

The problem is that after rotaing back to portrait, the layer is 'travelling' ie the x,y offset are gradually changing(increasing x, decreasing y). Scale seems fine (ie doesn't increment, decrement on repeated rotations)

Can anyone suggest a proper solution?

A: 

Perhaps it's due to the floating point rounding errors that are present in all implementations of floating-point numbers to some extent? Though if that's the case, your best bet may just be to do some sort of truncation or rounding somewhere in your code.

EDIT: Try doing something like using a smaller x translation on the transform back, or using a positive x translation and a non-zero y translation.

In fact, if the amount that is being translated by remains the same each time, then it probably is something to do with the translations you're giving it. You may be overcompensating for whatever required the translation in the first place, which could be the source of the problem.

JAB
no the offset with each rotate back to portrait is much much more than a floating point error
eco_bach
@echo_bach: I see, then. Reworking my answer.
JAB