Search code examples
iphoneanimationuiviewframecgaffinetransform

UIview core animation - rotate and setFrame at the same time


Hy,

I my application I want to animate an UIView by changing it's position and rotation at the same time. The issue is that sometimes the animation is changing the width and height of the UIView.

The animation code is this one:

int shuffleFrameX = (int)self.gameView.frame.size.width - (int)myView.frame.size.width;
int shuffleFrameY = (int)self.gameView.frame.size.height - (int)myView.frame.size.height;

int x = arc4random()%shuffleFrameX;
int y = arc4random()%shuffleFrameY;

CGRect newFrame = CGRectMake(x, y, myView.frame.size.width, myView.frame.size.height);

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];

if(isPieceRotationActive)
{
    int fact = arc4random()%4;
    myView.transform = CGAffineTransformMakeRotation(DegreesToRadians(90*fact));
    [myView setPieceRotation:fact];
}

[myView setFrame:newFrame];
[UIView commitAnimations];

This animation is repeated between 2 and 5 times using a the delegate functions. I did not copied them here.

Any idea what am I doing wrong?

Thanks!


Solution

  • I think you should be using bounds instead of frame. The frame will increase in size as a rotation is made on a rectangle to hold the rotated rectangle, the bounds will not since the bounds represents the internal coordinates of the rotated rectangle