我想制作一个简单的全屏 agar.io OS X 应用程序(没有标题,只是游戏),使用加载 agar.io 网站的 WebView...但与 Safari 相比,它遇到了严重的滞后问题好的...比如游戏无法玩。
为此,我创建了一个没有 Storyboard的简单 cocoa 应用程序(我尝试使用 Xcode 6.4 和 7 beta,在具有最佳显卡的最新 MacBook Pro 上的 Yosemite 上),向窗口添加一个 webview,连接 webview 导出。这是我的代码:// AppDelegate.swift
import Cocoa
import WebKit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var webView: WebView!
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
let frame = NSScreen.mainScreen()?.frame
window.setFrame(frame!, display: true)
let url = NSURL(string : "http://www.agar.io")
let request = NSURLRequest(URL: url!)
webView.mainFrame.loadRequest(request)
window.contentView = webView
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
也许我错过了什么?
最佳答案
我终于找到了解决方案...那就是使用新的 WKWebView 类而不是旧的 WebView 类。 WKWebView 类(几乎?)与 safari 一样快。
我的新代码几乎相同,只是我必须手动实例化 WKWebView,因为 UI 编辑器中没有 WKWebView 对象,并更改现在直接应用于 WKWebView 对象的 loadRequest(...) :
import Cocoa
import WebKit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
var webView:WKWebView?
func applicationDidFinishLaunching(aNotification: NSNotification) {
let url = NSURL(string : "http://agar.io")
let request = NSURLRequest(URL: url!)
webView = WKWebView()
webView!.loadRequest(request)
window.contentView = webView
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
关于macos - OSX WebView 非常慢且滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32290198/