Andrei Neacsu
Andrei Neacsu

Reputation: 1473

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!

Upvotes: 1

Views: 2698

Answers (2)

Roberson Balbino
Roberson Balbino

Reputation: 397

Change/Adapt your code to use this sequence:

  • Put view in original position(0 degree)
  • set new frame
  • rotate to desired degree.

Something like this:

  • myView.transform = CGAffineTransformMakeRotation(DegreesToRadians(0));
  • [myView setFrame:newFrame];
  • myView.transform = CGAffineTransformMakeRotation(DegreesToRadians(90*fact));

Good luck!

Upvotes: 2

Jamie
Jamie

Reputation: 5112

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

Upvotes: 2

Related Questions