好的,在我的应用程序上,当用户单击一个按钮时,将从服务器流式传输视频并在 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")
}
}
我收到此错误:
最佳答案
首先在按钮中设置通知
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)
}
现在创建方法并根据您的要求设置方向
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/