使用 CGAffine 旋轉視圖後的平移 (translation after rotation view using CGAffine)


問題描述

使用 CGAffine 旋轉視圖後的平移 (translation after rotation view using CGAffine)

I have a view that contain some textfield.  I set view's orientation like this 

(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]; 
    }
}

my problem is I want to make that view move upward when the keyboard appear, because some textfields are hidden by the keyboard. I think i have to use CGAffineTransformMakeTranslation, but i dont know how to use it after that rotation.

can somebody help me solve this problem??


參考解法

方法 1:

we can use CGAffineTransformConcat. Here is the code i made to solve this problem

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

but i still not understand, why the translation in portrait orientation && portrait upside down orientation have to use different point x and y. It's happen too in landscape left && landscape right orientation 

方法 2:

Here is how to rotate to the right

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

The, rotate to the left:

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

(by R. DewiR. DewiP.J.Radadiya)

參考文件

  1. translation after rotation view using CGAffine (CC BY-SA 3.0/4.0)

#cgaffinetransform #Translation #iOS #rotation






相關問題

CGAffineTransform:將比例應用於翻譯,如何? (CGAffineTransform: apply a Scale to a Translation, how?)

Facebook不尊重AVMutableCompositionTrack上的preferredTransform標誌 (Facebook not Respecting preferredTransform flag on AVMutableCompositionTrack)

使用 CGAffineTransform 時限制 UIImageView 的大小 (Limitting UIImageView size while using CGAffineTransform)

Выгляд павернутай табліцы не бачны ў iOS 6.0 (Rotated Table View is not visible in iOS 6.0)

使用 CGAffineTransformRotate,如何將旋轉設置為 0 (Using CGAffineTransformRotate, how to set rotation to 0)

Hành vi CGAffineTransformRotate không nhất quán giữa chế độ xem và bộ điều khiển chế độ xem (CGAffineTransformRotate behaviour inconsistent between view and view controller)

將 UIView.transform 設置為任意翻譯 CGAffineTransform 什麼都不做 (Setting UIView.transform to arbitrary translate CGAffineTransform does nothing)

使用 CGAfflineTransformMakeScale/Rotation 只做一個動作 (Using CGAfflineTransformMakeScale/Rotation only does one action)

使用 CGAffine 旋轉視圖後的平移 (translation after rotation view using CGAffine)

NSViews 上的 CGAffineTransforms (CGAffineTransforms on NSViews)

旋轉後向一個方向縮放 UIView 時,如何防止傾斜? (How can I prevent skew when scaling a UIView in one direction after rotation?)

使用捏合手勢縮放 UIImageView 的問題 (Problem scaling UIImageView with Pinch Gesture)







留言討論