【问题标题】:How to change rotation of icons in landscape mode in Swift?如何在 Swift 中更改横向模式下的图标旋转?
【发布时间】:2016-12-10 02:59:31
【问题描述】:

目前我的应用在纵向模式下如下所示:

但是当我的 iPad 处于横向模式时,我希望我的图标看起来像这样:

现在它在横向模式下看起来像这样:

我知道如何旋转图标,但我不知道我必须在哪里粘贴代码。我只想在横向模式下查看我的应用,就像第二张图片一样。

编辑

它应该看起来像这样:

【问题讨论】:

    标签: ios swift orientation


    【解决方案1】:

    您需要使用 NSNotificationCenter 以下代码设置观察者

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

    在 AppDelegate didFinishedLaunching 方法中。

    然后在观察者方法中检查方向

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

    【讨论】:

    • 您也可以在任何 UIViewController viewDidLoad 方法中执行此代码并为其添加观察者。
    【解决方案2】:

    如果您的deployment target 等于或大于8.0,您应该从Assets 管理它。您可以为不同的尺寸等级设置不同的图像。例如,对于 iPhone 中的纵向模式,您可以将资产设置为Compact width Regular Height 大小类,对于横向模式的 iphone,您可以将资产设置为Any width Compact Height 大小类。

    参考苹果文档Customizing Assets for Size Classes

    【讨论】:

    • 有没有办法覆盖旋转。我的应用会自动旋转,然后它看起来像我发布的第三张图片,而不是第二张
    • 如果您为横向和纵向设置了不同的图片,那么它将自动将其用于各自的方向。您无需以编程方式执行任何操作。
    • 为什么是不同的图片?!上面我发布了图片它的实际外观以及我想要它的外观
    • 第二件事应该通过自动布局来完成。如果您设置适当的约束,那么它将自动调整横向和纵向。
    • 我该如何设置约束,以便它们适用于横向和纵向模式?纵向图标高度为 view.height/2 和横向图标高度,如您在第三张图片中所见。我该如何解决?
    【解决方案3】:

    您需要在横向模式下将 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")
                }
    
            }
    

    希望这对其他人有帮助!

    【讨论】:

      【解决方案4】:

      在您的 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

      【讨论】:

      • 我已经编辑了我的帖子以便更好地理解。也许你知道应该怎么做?
      • @mafioso 我想你是在谈论支持你的应用程序的横向模式。
      • 支持横向模式,但图标看起来像第三张图片,我希望它们看起来像第四张图片
      • 在您的回答中,图标看起来像我想要的,但视图不是横向模式。我怎样才能把它结合起来?
      猜你喜欢
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      相关资源
      最近更新 更多