开发者

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 = arc4ran开发者_如何学编程dom()%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!


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


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜