代码之家  ›  专栏  ›  技术社区  ›  TheRedCamaro3.0 3.0

当设备在纵向模式下旋转而不锁定设备时,如何将collectionView的所有项目固定到位?

  •  1
  • TheRedCamaro3.0 3.0  · 技术社区  · 7 年前

    我想建立一个collectionView,当用户旋转设备而不将设备的方向锁定为纵向模式时,它可以将所有单元格保持在适当的位置。我试过使布局无效,但没有用。谢谢你的帮助。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Ashley Mills    7 年前

    根据您的意见,您希望将所有屏幕保持为纵向,但如果知道设备是否处于横向而不旋转界面(对于相机屏幕),您应该

    • 设置 支持的接口方向 在里面 your info.plist
    • 使用检测设备方向 CMMotionManager

      let motionManager = CMMotionManager()
      var currentOrientation = UIDeviceOrientation.portrait
      
      func montiorOrientation() {
          motionManager.startAccelerometerUpdates(to: OperationQueue()) { data, error in
              if let error = error {
                  print("CM orientation error - \(error)")
                  return
              }
              let acceleration = data!.acceleration
      
              let orientation: UIDeviceOrientation = fabs(acceleration.y) < fabs(acceleration.x) ?
                  (acceleration.x > 0 ? .landscapeRight : .landscapeLeft) :
                  (acceleration.y > 0 ? self.currentOrientation : .portrait) // Ignore portraitUpsideDown
      
              if orientation != self.currentOrientation {
                  DispatchQueue.main.async {
                      self.currentOrientation = orientation
                  }
              }
      
          }
      }