Swift Get Text Selection Events for UIWebView in Cordova Ion



目前,我有一个 Cordova ionic-angularjs 应用程序,我想在用户选择文本时冻结滚动(我按照这个小技巧启用上下文菜单(

现在,我有 Swift 本机代码捕获UIMenuController.didShowMenuNotificationUIMenuController.didHideMenuNotification事件,这反过来又调度了要由 Web 应用程序处理的相应 javascript 文档事件并冻结$ionicScrollDelegate,如下所示。这很好用,滚动在显示上下文菜单时被冻结,但是,如果用户想要扩展/收缩选择,上下文菜单会在扩展/收缩期间暂时消失,这会解冻滚动视图,直到上下文菜单在用户抬起手指后重新出现。是否可以获取所选文本的范围,而不是将滚动视图的冻结基于上下文菜单显示状态,以便冻结滚动视图,直到没有选择任何文本?

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
}
}

索引.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);
})

最新更新