无法访问 MTKView
Can't access MTKView
我是 UIKit 的新手,我正在尝试在 iPhone 12 上为 运行 编写一个 Metal 应用程序。这是 AppDelegate.swift 中的代码:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
}
这是 ViewController.swift 中的代码:
class ViewController: UIViewController, MTKViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Access the Metal view
if let view = self.view! as? MTKView { // Isn't working
view.device = MTLCreateSystemDefaultDevice()
view.delegate = self
print("Obtained MTKView")
} else {
print("Failed to obtain MTKView") // The result
}
}
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
func draw(in view: MTKView) {}
}
此代码编译 运行,但它不访问 MTKView
。它打印 Failed to obtain MTKView
消息。有任何想法吗?谢谢。
编辑:我试过 self.view!
和 self.view
,但都没有用。
如果您不使用情节提要,可以像这样创建一个新的 MTKView
:
class ViewController: UIViewController, MTKViewDelegate {
var metalView: MTKView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
/// Make a MetalKit view
let metalView = MTKView()
/// add positioning constraints
metalView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
metalView.topAnchor.constraint(equalTo: view.topAnchor),
metalView.rightAnchor.constraint(equalTo: view.rightAnchor),
metalView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
metalView.leftAnchor.constraint(equalTo: view.leftAnchor)
])
metalView.device = MTLCreateSystemDefaultDevice()
metalView.delegate = self
self.metalView = metalView /// assign to the property, so you can access this later
}
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
func draw(in view: MTKView) {}
}
我是 UIKit 的新手,我正在尝试在 iPhone 12 上为 运行 编写一个 Metal 应用程序。这是 AppDelegate.swift 中的代码:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
}
这是 ViewController.swift 中的代码:
class ViewController: UIViewController, MTKViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Access the Metal view
if let view = self.view! as? MTKView { // Isn't working
view.device = MTLCreateSystemDefaultDevice()
view.delegate = self
print("Obtained MTKView")
} else {
print("Failed to obtain MTKView") // The result
}
}
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
func draw(in view: MTKView) {}
}
此代码编译 运行,但它不访问 MTKView
。它打印 Failed to obtain MTKView
消息。有任何想法吗?谢谢。
编辑:我试过 self.view!
和 self.view
,但都没有用。
如果您不使用情节提要,可以像这样创建一个新的 MTKView
:
class ViewController: UIViewController, MTKViewDelegate {
var metalView: MTKView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
/// Make a MetalKit view
let metalView = MTKView()
/// add positioning constraints
metalView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
metalView.topAnchor.constraint(equalTo: view.topAnchor),
metalView.rightAnchor.constraint(equalTo: view.rightAnchor),
metalView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
metalView.leftAnchor.constraint(equalTo: view.leftAnchor)
])
metalView.device = MTLCreateSystemDefaultDevice()
metalView.delegate = self
self.metalView = metalView /// assign to the property, so you can access this later
}
func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
func draw(in view: MTKView) {}
}