【发布时间】:2020-02-20 01:17:14
【问题描述】:
目前,我有一个 Cordova ionic-angularjs 应用程序,我想在用户选择文本时冻结滚动(我通过关注 this little hack 启用上下文菜单)
现在,我有 Swift 本机代码捕获 UIMenuController.didShowMenuNotification 和 UIMenuController.didHideMenuNotification 事件,然后分派相应的 javascript 文档事件以由 Web 应用程序处理并冻结 $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);
})
【问题讨论】:
标签: swift cordova ionic-framework scroll contextmenu