2017-10-09 3 views
0

Derzeit i Kraft Orientierungsänderung machen, wenn AVPlayer Vollbild-Taste versucht pressed.I etwas ähnlichesForce-Orientierung funktioniert nicht in iOS 10 Geräte

final class NewMoviePlayerViewController: AVPlayerViewController { 
    override func viewDidLayoutSubviews() { 
     super.viewDidLayoutSubviews() 
     if contentOverlayView?.bounds == UIScreen.main.bounds{ 
      DispatchQueue.main.async { 
       let value = UIInterfaceOrientation.landscapeRight.rawValue 
       UIDevice.current.setValue(value, forKey: "orientation") 
      } 
      print("full screen") 
     }else{ 
      DispatchQueue.main.async { 
      let value = UIInterfaceOrientation.portrait.rawValue 
      UIDevice.current.setValue(value, forKey: "orientation") 
      } 
      print("half screen") 
     } 
    } 

} 

Der obige Code funktioniert gut in iOS 11, aber den gleichen Code nicht in iOS arbeiten 10 und unterhalb Versionen

+0

'contentOverlayView .bounds == UIScreen.main.bounds' in IOS wahr ist 10 (und unten) oder das Problem ist im Inneren' UIDevice.current.setValue (Wert, forKey: "Orientierung")? ' ? –

+0

@AlbertoCantallops die Bedingung geht nach innen, aber Kraft Orientierung Code funktioniert nicht – karthikeyan

+0

werfen Sie einen Blick https://StackOverflow.com/Questions/26357162/How-to-force-view-Controller-orientation-in-ios-8 gibt es eine Reihe verschiedener Ansätze. Beachten Sie die angenommene Antwort ist, wie Sie es getan haben, aber https://stackoverflow.com/a/28220616/7064692 könnte vielleicht helfen –

Antwort

1

Wir haben es schließlich mit folgenden Art und Weise festgelegt ...

Wir haben eine Bool Variable auf APPD genommen elegate, um eine Orientierungsänderung zu erkennen.

var isLandScapeManualCheck = Bool() 

Als nächstes werden wir Anwendung folgende Delegierten

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 

     if isLandScapeManualCheck == false{ 
     return UIInterfaceOrientationMask.portrait 
     }else{ 

      return UIInterfaceOrientationMask.landscapeRight 
     } 
//  return UIInterfaceOrientationMask.portrait 

    } 

Basierend auf Bool Wert haben wir Rückkehr aus Orientierung Modus implementiert haben.

iOS 10 und unten Version sollte auf diese Weise folgen ..

In Ihrem playercontroller Blick .. (bedeutet, dass Ihre Heimsteuerung)

if #available(iOS 11, *) { 

       }else{ 
        playerVwController.contentOverlayView!.addObserver(self, forKeyPath: "bounds", options: NSKeyValueObservingOptions.new, context: nil) 
       } 



override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 
     if keyPath == "bounds"{ 
      let rect = change![.newKey] as! NSValue 

      if let playerRect: CGRect = rect.cgRectValue as CGRect { 
       if playerRect.size == UIScreen.main.bounds.size { 
        print("Player in full screen") 
        let value = UIInterfaceOrientation.landscapeLeft.rawValue 
        UIDevice.current.setValue(value, forKey: "orientation") 
        isLandScapeManualCheck = true 
       } else { 
        DispatchQueue.main.async { 
        let value = UIInterfaceOrientation.portrait.rawValue 
        UIDevice.current.setValue(value, forKey: "orientation") 
        print("Player not in full screen") 
        isLandScapeManualCheck = false 
        } 

       } 
      } 
     } 
    } 

Nach iOS 11

Sie in viewDidLayoutSubviews nennen sollten

UIViewController.attemptRotationToDeviceOrientation() 

So endlich Ihr Sub-Klasse-Code wie Belo w

final class NewMoviePlayerViewController: AVPlayerViewController { 
    override func viewDidLayoutSubviews() { 
     UIViewController.attemptRotationToDeviceOrientation() 
     super.viewDidLayoutSubviews() 
     if contentOverlayView?.bounds == UIScreen.main.bounds{ 
      DispatchQueue.main.async { 
       let value = UIInterfaceOrientation.landscapeRight.rawValue 
       UIDevice.current.setValue(value, forKey: "orientation") 
      } 
//   self.contentOverlayView?.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi/2)) 
      print("full screen") 
     }else{ 
      DispatchQueue.main.async { 
      let value = UIInterfaceOrientation.portrait.rawValue 
      UIDevice.current.setValue(value, forKey: "orientation") 
      } 
//   self.contentOverlayView?.transform = CGAffineTransform.identity 

      print("half screen") 
     } 

    } 

} 
Verwandte Themen