Swift 在 Cordova Ionic 应用程序中获取 UIWebView 的文本选择事件
Swift Get Text Selection Events for UIWebView in Cordova Ionic Application
目前,我有一个 Cordova ionic-angularjs 应用程序,我想在用户选择文本时冻结滚动(我通过以下 this little hack 启用上下文菜单)
现在,我有 Swift 本机代码捕获 UIMenuController.didShowMenuNotification
和 UIMenuController.didHideMenuNotification
事件,这些事件依次分派各自的 javascript 文档事件,由Web 应用程序并冻结 $ionicScrollDelegate
,如下所示。这很好用,滚动在显示上下文菜单时被冻结,但是,如果用户想要 expand/contract 选择,上下文菜单会在 expansion/contraction 期间暂时消失,这会解冻滚动视图,直到用户抬起手指后上下文菜单重新出现。是否可以获取所选文本的范围,而不是根据上下文菜单显示状态冻结滚动视图,从而冻结滚动视图,直到没有选择文本?
CDVContextMenu.swift
@objc(CDVContextMenu)
class CDVContextMenu: CDVPlugin {
typealias This = CDVContextMenu
static var sharedCommandDelegate: CDVCommandDelegate?
var contextMenuVisible = false
override func pluginInitialize() {
super.pluginInitialize()
This.sharedCommandDelegate = commandDelegate
NotificationCenter.default
.addObserver(self,
selector: #selector(menuDidShow),
name: UIMenuController.didShowMenuNotification,
object: nil)
NotificationCenter.default
.addObserver(self,
selector: #selector(menuDidHide),
name: UIMenuController.didHideMenuNotification,
object: nil)
}
// MARK: - Event Handlers
@objc
func menuDidShow(_ notification: Notification) {
This.sharedCommandDelegate?.evalJs("document.dispatchEvent(new Event('contextMenuDidShow'));")
contextMenuVisible = true
}
@objc
func menuDidHide(_ notification: Notification) {
This.sharedCommandDelegate?.evalJs("document.dispatchEvent(new Event('contextMenuDidHide'));")
contextMenuVisible = false
}
}
index.js
document.addEventListener('contextMenuDidShow', function() {
$ionicScrollDelegate.freezeScroll(true);
})
document.addEventListener('contextMenuDidHide', function() {
$ionicScrollDelegate.freezeScroll(false);
})
我在 javascript 中找到了解决方法,不需要插件:
document.addEventListener('selectionchange', function() {
$ionicScrollDelegate.freezeScroll(true);
});
document.addEventListener('touchend', function() {
$ionicScrollDelegate.freezeScroll(false);
})
目前,我有一个 Cordova ionic-angularjs 应用程序,我想在用户选择文本时冻结滚动(我通过以下 this little hack 启用上下文菜单)
现在,我有 Swift 本机代码捕获 UIMenuController.didShowMenuNotification
和 UIMenuController.didHideMenuNotification
事件,这些事件依次分派各自的 javascript 文档事件,由Web 应用程序并冻结 $ionicScrollDelegate
,如下所示。这很好用,滚动在显示上下文菜单时被冻结,但是,如果用户想要 expand/contract 选择,上下文菜单会在 expansion/contraction 期间暂时消失,这会解冻滚动视图,直到用户抬起手指后上下文菜单重新出现。是否可以获取所选文本的范围,而不是根据上下文菜单显示状态冻结滚动视图,从而冻结滚动视图,直到没有选择文本?
CDVContextMenu.swift
@objc(CDVContextMenu)
class CDVContextMenu: CDVPlugin {
typealias This = CDVContextMenu
static var sharedCommandDelegate: CDVCommandDelegate?
var contextMenuVisible = false
override func pluginInitialize() {
super.pluginInitialize()
This.sharedCommandDelegate = commandDelegate
NotificationCenter.default
.addObserver(self,
selector: #selector(menuDidShow),
name: UIMenuController.didShowMenuNotification,
object: nil)
NotificationCenter.default
.addObserver(self,
selector: #selector(menuDidHide),
name: UIMenuController.didHideMenuNotification,
object: nil)
}
// MARK: - Event Handlers
@objc
func menuDidShow(_ notification: Notification) {
This.sharedCommandDelegate?.evalJs("document.dispatchEvent(new Event('contextMenuDidShow'));")
contextMenuVisible = true
}
@objc
func menuDidHide(_ notification: Notification) {
This.sharedCommandDelegate?.evalJs("document.dispatchEvent(new Event('contextMenuDidHide'));")
contextMenuVisible = false
}
}
index.js
document.addEventListener('contextMenuDidShow', function() {
$ionicScrollDelegate.freezeScroll(true);
})
document.addEventListener('contextMenuDidHide', function() {
$ionicScrollDelegate.freezeScroll(false);
})
我在 javascript 中找到了解决方法,不需要插件:
document.addEventListener('selectionchange', function() {
$ionicScrollDelegate.freezeScroll(true);
});
document.addEventListener('touchend', function() {
$ionicScrollDelegate.freezeScroll(false);
})