2016-08-04 32 views
0

目前我的應用程序看起來像這樣在人像模式: enter image description here如何在Swift中更改橫向模式下的圖標旋轉?

但是當我的iPad是在橫向模式我想我的圖標看起來像這樣: enter image description here

現在看起來,在橫向模式:

enter image description here

我知道如何旋轉圖標,但我不知道我必須粘貼代碼。我只想看看我的應用程序在橫向模式,如第二張圖片。

編輯

看起來應該像這樣的: enter image description here

回答

1

您需要旋轉你的yourimageview 90度橫向模式 -

在AppDelegate.swift內"didFinishLaunchingWithOptions"功能我把:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil) 

然後AppDelegate類中,我把下列功能:

 func rotated() 
     { 
      if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)) 
      {    
       print("landscape") 
      //Rotate 90 degrees clockwise:   
       yourimageview1.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))   
      //Rotate 90 degrees counterclockwise:   
      yourimageview2.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2)) 
      } 
      if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)) 
      { 
       print("Portrait") 
      } 

     } 

希望這有助於任何人!

+0

我不知道我應該在哪裏粘貼代碼 – mafioso

+0

我編輯的帖子看看現在 – mafioso

3

你需要使用NSNotificationCenter在AppDelegate中設置觀察下面的代碼

NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationChnaged", name: UIDeviceOrientationDidChangeNotification, object: nil) 

didFinishedLaunching方法。

而在觀察者方法您檢查方向

if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)) 
{    
    print("landscape") 
} 

if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)) 
{ 
    print("Portrait") 
} 
+0

你也可以這樣做在任何UIViewController viewDidLoad方法中添加代碼併爲其添加觀察者。 – RBN

1

您應該Assets管理它,如果你deployment target等於或大於8.0更大。您可以爲不同尺寸的類別設置不同的圖像。例如,對於iphone中的肖像模式,您可以設置Compact width Regular Height尺寸等級的分類和風景中的IPhone,您可以爲Any width Compact Height尺寸等級設置資產。

請參考蘋果文檔Customizing Assets for Size Classes

+0

有沒有辦法來覆蓋旋轉。我的應用程序自動旋轉,然後它看起來像我張貼的第三張照片,不像第二張 – mafioso

+0

如果您爲橫向和縱向設置了不同的照片,那麼它會自動將其取向爲相應的方向。你不需要以編程方式做任何事情。 – Lion

+0

爲什麼不同的圖片?上面我張貼了圖片,它實際上是什麼樣子以及我想要它看起來像什麼 – mafioso

0

嘗試如下您ViewController.m

-(void)viewDidLoad 
{ 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationChnaged", name: UIDeviceOrientationDidChangeNotification, object: nil) 
} 


- (void) deviceOrientationChnaged:(NSNotification *) notification 
{ 
    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)) 
    {    
     print("landscape") 
     yourimageview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) 

     //OR Rotate 90 degrees counterclockwise: 

     yourimageview.transform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2)) 

    } 

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)) 
    { 
     print("Portrait") 
    } 
} 

希望這會有所幫助。

編輯:

請檢查我這樣一個問題:LINK

+0

我編輯了我的帖子以便更好地理解。也許你知道該怎麼辦? – mafioso

+0

@mafioso我認爲您正在討論支持橫向模式的應用程序。 – Rohan

+0

@mafioso查看我更新的答案 – Rohan

相關問題