[英]Rotating an image 90 degrees on same position when orientation changes
結束這樣做: 支持多個方向的最簡單方法? 當應用程序在Landscape中時如何加載自定義NIB? 工作精湛!
我在Photoshop中制作了一個圖像,我想在iPad應用程序中將其用作信息屏幕的背景。 該圖像還包含文本和一些圖標。 在圖像周圍,我有一個綠色的邊框。
我想要達到的效果是:
當用戶從縱向朝向橫向時,我希望圖像(僅框架和圖標)旋轉90度,使圖像以橫向模式顯示,而不是在橫向上具有框架的縱向視圖。 文本和圖標是分離的(我在不同的UIImageView中組織的不同層)它們將旋轉90度。
我已經完成的工作如下:
用這種方法進行了一些實驗:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:
並試圖這樣做:
self.btnFacebook.transform = CGAffineTransformMakeRotation(1.5707964);
我認為它會將btnFacebook屬性向右旋轉90度(如果我錯了,請糾正我)因為它指定了正弧度。 我似乎無法讓它正常工作。 難道這不能將按鈕繞框架中心坐標旋轉90度嗎? 這不會導致按鈕位置發生變化(按鈕是方形)?
制作圖像:
由於圖像顯示圖像背景(其上的一些自定義圖形在兩個方向上看起來都很好)從縱向到橫向並旋轉,因此它在橫向上不顯示為肖像,圖標與背景分離,因此它們需要旋轉為因為他們需要處於正確的方向(這是社交圖標)。 然而,文本位於相同的位置,它只旋轉90度而不重新定位。
我理解你的問題是你試圖不旋轉整個界面,而是單獨旋轉紫色和紅色方塊。
我創建了一個類似於你的布局的UIViewController
。
黑色正方形是一個UIView,白色正方形只有這樣我才能知道黑色視圖何時旋轉。 此視圖連接到控制器上的view1
屬性。
有4個按鈕連接到btnx
(x運行1到4)屬性。
由於我不再想要自動旋轉界面,我只支持縱向方向。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
為了手動完成旋轉,我向ViewController添加了一個方法。 它確定將組件從縱向旋轉到當前方向所需的角度,創建旋轉變換並將其應用於所有出口。
- (void)deviceDidRotate:(NSNotification *)notification
{
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
double rotation = 0;
UIInterfaceOrientation statusBarOrientation;
switch (currentOrientation) {
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationUnknown:
return;
case UIDeviceOrientationPortrait:
rotation = 0;
statusBarOrientation = UIInterfaceOrientationPortrait;
break;
case UIDeviceOrientationPortraitUpsideDown:
rotation = -M_PI;
statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
break;
case UIDeviceOrientationLandscapeLeft:
rotation = M_PI_2;
statusBarOrientation = UIInterfaceOrientationLandscapeRight;
break;
case UIDeviceOrientationLandscapeRight:
rotation = -M_PI_2;
statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
break;
}
CGAffineTransform transform = CGAffineTransformMakeRotation(rotation);
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.btn1 setTransform:transform];
[self.btn2 setTransform:transform];
[self.btn3 setTransform:transform];
[self.btn4 setTransform:transform];
[self.view1 setTransform:transform];
[[UIApplication sharedApplication] setStatusBarOrientation:statusBarOrientation];
} completion:nil];
}
最后要做的是讓操作系統調用我的方法。 為此,我在application:didFinishLaunchingWithOptions:
添加了以下代碼application:didFinishLaunchingWithOptions:
在AppDelegate中。
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self.viewController selector:@selector(deviceDidRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
我不確定這是否正是你想要的,但我相信它至少是相似的,所以你可以從中得到一些想法如何解決你的問題。 我可以為我創建的工作iPad應用程序提供源代碼來說明這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.