ios - Swift - MPMoviePlayerViewController 支持的设备方向

标签 ios swift orientation mpmovieplayercontroller

好的,在我的应用程序上,当用户单击一个按钮时,将从服务器流式传输视频并在 MPMoviePlayerViewController 中打开。

这是用户点击按钮时的代码:

@IBAction func WatchPressed(sender: AnyObject)
{self.presentMoviePlayerViewControllerAnimated(movieViewController)}

基本上我想知道我是否可以锁定放置按钮的主视图 Controller 的方向,然后在呈现 MPMoviePlayerViewController 时支持所有方向?如果可以,我可以使用标准支持的设备方向代码吗?

编辑:

使用此代码时:

    var movieplayer : MPMoviePlayerController!
    @IBAction func WatchPressed(sender: AnyObject)
    {self.presentMoviePlayerViewControllerAnimated(movieViewController)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "Rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
    }
}


func Rotated()
{
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 4 ||  UIDevice.currentDevice().orientation.rawValue == 3
        {
            movieplayer.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

        }
    }
    else if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 1 || UIDevice.currentDevice().orientation.rawValue == 2
        {
            movieplayer.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

            let value = UIInterfaceOrientation.Portrait.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }

    }

我收到此错误: enter image description here

最佳答案

首先在按钮中设置通知

override func viewWillAppear(animated: Bool) {
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

@IBAction func WatchPressed(sender: AnyObject)
{
 self.presentMoviePlayerViewControllerAnimated(movieViewController)
 NSNotificationCenter.defaultCenter().addObserver(self, selector: "Rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
}

enter image description here

现在创建方法并根据您的要求设置方向

func Rotated()
{
if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 4 ||  UIDevice.currentDevice().orientation.rawValue == 3
        {
           movieViewController!.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

        }
    }
    else if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 1 || UIDevice.currentDevice().orientation.rawValue == 2
        {
          movieViewController!.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

            let value = UIInterfaceOrientation.Portrait.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }

    }
}

希望对你有帮助!!!!!!

关于ios - Swift - MPMoviePlayerViewController 支持的设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30666040/

相关文章:

Android 将设备坐标系转换为 "user"坐标系

ios - 如何更改重复单元格中点击的按钮图像?

ios - 没有挑战的登录表单(适配器)

ios - 获取适用于 iOS 的 YouTube 视频说明 (YouTube API)

ios - Xcode:AccessoryType.checkmark 将其他 View 推到左侧

ios - 笑脸评分栏 swift

android - 在 onPause() 内调用 finish() 但在方向改变时不调用

ios - 从委托(delegate)函数 Swift 4 中提取值

iphone - 删除单元格和重新加载 TableView 时访问权限不佳

Android - 更改模拟器屏幕方向