[目前,我有一个Cordova ionic-angularjs应用程序,我想在其中冻结滚动,而用户正在选择文本(我通过按this little hack启用上下文菜单)
[现在,我具有捕获UIMenuController.didShowMenuNotification
和UIMenuController.didHideMenuNotification
事件的Swift本机代码,它们依次分派了要由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
}
}
index.js
document.addEventListener('contextMenuDidShow', function() {
$ionicScrollDelegate.freezeScroll(true);
})
document.addEventListener('contextMenuDidHide', function() {
$ionicScrollDelegate.freezeScroll(false);
})
我在不需要插件的情况下通过javascript找到了解决方法:
menu.isSelectingText = false;
document.addEventListener('selectionchange', function() {
menu.isSelectingText = true;
$ionicScrollDelegate.freezeScroll(true);
});
document.addEventListener('touchend', function() {
if (!menu.isSelectingText) return;
menu.isSelectingText = false;
$ionicScrollDelegate.freezeScroll(false);
})