【问题标题】:Swift Get Text Selection Events for UIWebView in Cordova Ionic ApplicationSwift 在 Cordova Ionic 应用程序中获取 UIWebView 的文本选择事件
【发布时间】:2020-02-20 01:17:14
【问题描述】:

目前,我有一个 Cordova ionic-angularjs 应用程序,我想在用户选择文本时冻结滚动(我通过关注 this little hack 启用上下文菜单)

现在,我有 Swift 本机代码捕获 UIMenuController.didShowMenuNotificationUIMenuController.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


    【解决方案1】:

    我通过在 javascript 中执行此操作而不需要插件找到了解决方法:

    document.addEventListener('selectionchange', function() {
        $ionicScrollDelegate.freezeScroll(true);
    });
    document.addEventListener('touchend', function() {
        $ionicScrollDelegate.freezeScroll(false);
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 2016-01-11
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 2021-10-28
      相关资源
      最近更新 更多