1 屏幕旋转和全屏:
首先必须在根控制器中设置shouldAutorotate,
如果根控制器设置为self.window?.rootViewController = BaseNavigationController(rootViewController: ViewController())
则必须在BaseNavigationController内设置,想要在不同控制器中自定义是否选装,女啊中代码如下
override func shouldAutorotate() -> Bool {
return (self.topViewController?.shouldAutorotate()) ?? false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (self.topViewController?.supportedInterfaceOrientations()) ?? [.Portrait]
}
在特定控制器中代码:其中view为你需要放大缩小全屏展示的view
override func shouldAutorotate() -> Bool {
return true
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
super.willRotateToInterfaceOrientation(toInterfaceOrientation, duration: duration)
switch toInterfaceOrientation {
case .Unknown:
print("未知方向")
break
case .LandscapeLeft:
print("屏幕向左横置")
view.frame = CGRectMake(0, 0, P.height , P.width)
self.verticalView.hidden = true
break
case .LandscapeRight:
print("屏幕向右横置")
view.frame = CGRectMake(0, 0, P.height , P.width)
break
case .Portrait:
view.frame = CGRectMake(0, 44, P.width , P.width * scale)
print("屏幕直立")
break
case .PortraitUpsideDown:
print("屏幕直立,上下颠倒")
break
default:
print("无法辨别")
break
}
}
主动控制旋转:
在播放界面上添加一个透明模版,然后在模版上添加你需要的按钮,比如音量,全屏按钮,这样可以有效低耦合,按钮功能与播放器分别管理;
verticalView为朦层view
代理方法:
let sele = self.verticalView.fullScreenButton.selected
self.verticalView.fullScreenButton.selected = !sele
print("select = (sele)")
if self.verticalView.fullScreenButton.selected {
setNewOrientation(true)
} else {
setNewOrientation(false)
}
按钮click:setNewOrientation
func setNewOrientation(fullScreen: Bool) {
let device = UIDevice.currentDevice()
let lastOrientation = device.orientation
let deviceOrientation = fullScreen ? UIInterfaceOrientation.LandscapeRight : UIInterfaceOrientation.Portrait
if device.respondsToSelector("setOrientation:") {
device.setValue(deviceOrientation.rawValue, forKey: "orientation")
}
if lastOrientation.rawValue == deviceOrientation.rawValue {
UIViewController.attemptRotationToDeviceOrientation()
}
}
2 音量控制:
通遍历volumeView来获取其UISlider类型的子控件来实现音量的控制(设计私有方法)
for item in volumeView.subviews {
if item.isKindOfClass(UISlider) {
self.volumeSlider = item as! UISlider
}
}
func muteButtonButtonClick(btn: UIButton) {
let sele = btn.selected
btn.selected = !sele
print("value = (lastVolumeValue)")
//静音
if !sele {
lastVolumeValue = (volumeSlider?.value)!
self.volumeSlider?.value = 0
} else {
self.volumeSlider?.value = lastVolumeValue
}
}