使用CGAffine进行旋转视图后的平移

时间:2010-12-01 01:40:59

标签: ios rotation translation cgaffinetransform

我有一个包含一些文本字段的视图。我像这样设置了视图的方向

(void)deviceRotated:(id)sender
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation];

    if (orientation == UIDeviceOrientationPortrait)
    {
        CGAffineTransform affine = CGAffineTransformMakeRotation (0.0);
        [self.View setTransform:affine];
    }

    if (orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        CGAffineTransform affine = CGAffineTransformMakeRotation (M_PI * 180 / 180.0f);
        [self.View setTransform:affine];
    }
    else if (orientation == UIDeviceOrientationLandscapeLeft)
    {
        CGAffineTransform affine = CGAffineTransformMakeRotation (M_PI * 90 / 180.0f);  
        [self.View setTransform:affine];
    }
    else if (orientation == UIDeviceOrientationLandscapeRight)
    {
        CGAffineTransform affine = CGAffineTransformMakeRotation ( M_PI * 270 / 180.0f);
        [self.View setTransform:affine]; 
    }
}

我的问题是我想在键盘出现时让视图向上移动,因为某些文本字段被键盘隐藏了。我想我必须使用CGAffineTransformMakeTranslation,但我不知道在旋转之后如何使用它。

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:8)

我们可以使用CGAffineTransformConcat。这是我为解决这个问题而编写的代码

CGAffineTransform rotate = CGAffineTransformMakeRotation (0.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-5, -150);
self.alertView.transform = CGAffineTransformConcat(translate, rotate);

但我仍然不明白,为什么纵向翻译&&纵向颠倒方向必须使用不同的点x和y。它也是在景观中发生的左右&&景观正确的方向

答案 1 :(得分:0)

以下是如何向右旋转

CGAffineTransform translate = CGAffineTransformMakeRotation(M_PI/18);
view.transform = CGAffineTransformConcat(view.transform, translate);

向左旋转:

CGAffineTransform translate = CGAffineTransformMakeRotation(-M_PI/18);
view.transform = CGAffineTransformConcat(view.transform, translate);