旋转UIWebView嵌入视频的方法如下:
在UIViewController中,可以使用UIDevice的beginGeneratingDeviceOrientationNotifications方法来监听设备方向的变化。
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name: UIDevice.orientationDidChangeNotification, object: nil)
}
在UIViewController中,可以使用NotificationCenter来监听设备方向的变化。
@objc func orientationChanged() {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait:
// 竖屏
case .portraitUpsideDown:
// 倒竖屏
case .landscapeLeft:
// 左横屏
case .landscapeRight:
// 右横屏
default:
break
}
}
在UIViewController中,可以使用UIWebView的transform属性来旋转UIWebView。
@objc func orientationChanged() {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait:
// 竖屏
webView.transform = CGAffineTransform.identity
case .portraitUpsideDown:
// 倒竖屏
webView.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
case .landscapeLeft:
// 左横屏
webView.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
case .landscapeRight:
// 右横屏
webView.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)
default:
break
}
}
在UIViewController中,可以使用UIWebView的frame属性来调整UIWebView的大小和位置。
@objc func orientationChanged() {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait:
// 竖屏
webView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
case .portraitUpsideDown:
// 倒竖屏
webView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
case .landscapeLeft:
// 左横屏
webView.frame = CGRect(x: 0, y: 0, width: view.bounds.height, height: view.bounds.width)
case .landscapeRight:
// 右横屏
webView.frame = CGRect(x: 0, y: 0, width: view.bounds.height, height: view.bounds.width)
default:
break
}
}
在UIWebView中,可以使用JavaScript来旋转视频。
@objc func orientationChanged() {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait:
// 竖屏
webView.stringByEvaluatingJavaScript(from: "rotateVideo(0);")
case .portraitUpsideDown:
// 倒竖屏
webView.stringByEvaluatingJavaScript(from: "rotateVideo(180);")
case .landscapeLeft:
// 左横屏
webView.stringByEvaluatingJavaScript(from: "rotateVideo(270);")
case .landscapeRight:
// 右横屏
webView.stringByEvaluatingJavaScript(from: "rotateVideo(90);")
default:
break
}
}
import UIKit
import WebKit
class ViewController: UIViewController {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let configuration = WKWebViewConfiguration()
webView = WKWebView(frame: CGRect.zero, configuration: configuration)
webView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(webView)
let top = NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 0)
let bottom = NSLayoutConstraint(item: webView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0)
let leading = NSLayoutConstraint(item: webView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 0)
let trailing = NSLayoutConstraint(item: webView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1.0, constant: 0)
view.addConstraints([top, bottom, leading, trailing])
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(orientationChanged), name: UIDevice.orientationDidChangeNotification, object: nil)
let url = URL(string: "https://www.example.com")!
let request = URLRequest(url: url)
webView.load(request)
}
@objc func orientationChanged() {
let orientation = UIDevice.current.orientation
switch orientation {
case .portrait:
// 竖屏
webView.transform = CGAffineTransform.identity
webView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
webView.stringByEvaluatingJavaScript(from: "rotateVideo(0);")
case .portraitUpsideDown:
// 倒竖屏
webView.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
webView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
webView.stringByEvaluatingJavaScript(from: "rotateVideo(180);")
case .landscapeLeft:
// 左横屏
webView.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
webView.frame = CGRect(x: 0, y: 0, width: view.bounds.height, height: view.bounds.width)
webView.stringByEvaluatingJavaScript(from: "rotateVideo(270);")
case .landscapeRight:
// 右横屏
webView.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)
webView.frame = CGRect(x: 0, y: 0, width: view.bounds.height, height: view.bounds.width)
webView.stringByEvaluatingJavaScript(from: "rotateVideo(90);")
default:
break
}
}
}
这样,就可以在UIWebView中旋转嵌入视频了。
领取专属 10元无门槛券
手把手带您无忧上云