在iOS6中使用自动布局旋转和缩放

时间:2013-06-20 17:58:54

标签: ios objective-c rotation scale autolayout

这是我一直以来遇到的一个问题。使用新的iOS6和自动布局,我的手势识别器“游戏”不再有效。

我使用故事板创建了一个应用程序,并且在其中一个标签上有一个小游戏应用程序,用户必须在5像素内的另一个图像上缩放,旋转和平移图像, 5%大小,5度。我在自动布局中遇到的问题是,当旋转和缩放游戏图像时,图像会“跳跃”回到它的起始位置并且笨拙地旋转而不是围绕中心旋转。

我的处理旋转比例手势的方法如下。

旋转

//rotation gesture recognizer response
- (void)respondToRotateGesture:(UIRotationGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        image.transform = CGAffineTransformRotate(gesture.view.transform, gesture.rotation);
    }
    if (gesture.state == UIGestureRecognizerStateEnded) [self didWin]; //not important for question
    gesture.rotation = 0;
}

捏紧

//pinch gesture recognizer
- (void)respondToPinchGesture:(UIPinchGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
            image.transform = CGAffineTransformScale(image.transform, gesture.scale, gesture.scale);
    }
    if (gesture.state == UIGestureRecognizerStateEnded) [self didWin]; //not important for question
    gesture.scale = 1;
}

我确实认识到最简单的解决方案是关闭自动布局,但这会导致故事板的其余部分出现问题,因为在故事板中你不能为一个组件转动自动布局但是你必须为整个故事板关闭它。

任何人都可以告诉我该怎么做?我已经读到这是一个主要问题,有些人称它为新的自动布局的“bug”。

我只是遇到了问题,我需要一些帮助

如果有人有任何建议或能指出我正确的方向,我将不胜感激。谢谢!

-Henry

1 个答案:

答案 0 :(得分:3)

最简单的方法是使用以下代码以编程方式创建图像以删除自动布局。由于使用自动布局的苹果“bug”,如果使用IB将图像添加到视图中,则无法设置image.translatesAutoresizingMaskIntoConstraints = YES。因为您会收到错误消息“Unable to simultaneously satisfy constraints

因此,您需要以编程方式执行此操作,因为IB会添加约束而您不需要任何约束。

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    image.image = [UIImage imageNamed:@"gorilla_red.png"];
    image.translatesAutoresizingMaskIntoConstraints = YES;
    [self.view addSubview:image];