使用UIView动画改变它的框架来旋转UIImageView会改变它的大小

时间:2011-04-20 20:49:23

标签: objective-c ios4

我首先在第一次旋转UIImageView然后设置其框架动画时遇到麻烦。一旦我开始将它的帧设置为target_position的动画,我的图像就会改变它的大小。它越来越宽(取决于旋转角度)

我目前用于轮换的代码是:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!isAnimating) {
        isAnimating = YES;
        UITouch *t = [[touches allObjects] lastObject];

        float angle = atan2([t locationInView:self.view].y - player.frame.origin.y, [t locationInView:self.view].x - player.frame.origin.x) * 180 / M_PI;
        NSLog(@"Angle: %f", angle);

        CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(angle));

        [player setTarget_position:CGRectMake([t locationInView:self.view].x, [t locationInView:self.view].y, player.image.size.width-angle, player.image.size.height+angle)];

        [UIView beginAnimations:@"rotatePlayer" context:nil];
        [UIView setAnimationDuration:2];
        player.transform = cgCTM;
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(playerFinishedRotating)];
        [UIView commitAnimations];
    }
}

旋转本身运行良好。这是playerFinishedRotating函数的代码:

- (void)playerFinishedRotating {
    [UIView beginAnimations:@"rotatePlayer" context:nil];
    [UIView setAnimationDuration:3];
    [player setFrame:[player target_position]];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(playerFinishedAnimating)];
    [UIView commitAnimations];
}

对我来说,旋转它时我的UIImageVie的大小会发生变化是合乎逻辑的。但我仍然坚持如何使用给定的数据(旋转角度f.e。)调整target_position框架的宽度和高度值。

我不是数学天才,所以对于其他人来说答案可能是显而易见的,但对我来说并非如此:(

希望有人可以提供帮助,如果您有其他问题,请发表评论。

1 个答案:

答案 0 :(得分:0)

搜索了一下后,我在stackoverflow上找到了答案。任何有同样问题的人,请看这里:Rotating and Moving a UIImageView (CocoaTouch)