【问题标题】:Pointing a popup to point to the word tapped in a text view将弹出窗口指向文本视图中点击的单词
【发布时间】:2018-04-05 12:46:33
【问题描述】:

我很困惑如何在提供的屏幕截图上定位弹出窗口以指向点击的单词。文本视图中的单词是灰色的。我会展示一些代码,但除了处理水龙头的代码之外,我没有其他相关的代码。

这是一些代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    if(segue.identifier == "popOverSegue") {
        let destinationViewController: PopoverViewController = segue.destination as! PopoverViewController
        destinationViewController.modalPresentationStyle = UIModalPresentationStyle.popover
        destinationViewController.popoverPresentationController!.delegate = self
        destinationViewController.popoverPresentationController?.permittedArrowDirections = .down
        destinationViewController.tappedWord = tappedWord
        destinationViewController.definitionText = "This is a test description"

        let x = meaningText.frame.minX
        let y = meaningText.frame.minY

        destinationViewController.popupOrigin = CGPoint(x: x + pointOfTap.x,y:  y + pointOfTap.y)
        destinationViewController.popupSize = CGSize(width: 200, height: 200)


        debugPrint(meaningText)
    }
}

【问题讨论】:

    标签: ios iphone popup position


    【解决方案1】:

    不确定这些是否适用:

    如果您对突出显示的文本使用UIButton(),则可以使用以下内容轻松获取按钮的位置:

    let buttonLocation = theButton.frame.origin
    print("Buttons location is \(buttonLocation)")
    

    或者,如果您使用UIGestureRecognizer 打开弹出框,您可以使用location(in: UIView) 来获取点击位置(这将位于突出显示的文本上/附近的某个位置。

    会是这样的:

    override func viewDidLoad() {
        let tapView = UITapGestureRecognizer(target: self, action: #selector(TheVC.popover(recognizer:)))
        self.view.isUserInteractionEnabled = true
        self.view.addGestureRecognizer(tapView)
    }
    
    @objc func popover(recognizer: UIGestureRecognizer) {
        let tapLocation = recognizer.location(in: self.view)
        print("Tap occurred at location \(tapLocation)")
    }
    

    一旦您有了点击的位置或触发点击的 UI 元素,您就可以从那里计算弹出框的位置。

    【讨论】:

    • 计算单词相对于文本视图的位置是我很难计算的。我有用户点击这个词的地方,我有这个词本身。我只是 1) 似乎无法重新定位弹出窗口而不完全从屏幕上丢失它,并且 2) 无法获得文本视图相对于其视图控制器的位置。
    • 对于#1,在没有看到代码来查看您引用的内容的情况下,我会确保您根据 UIViewController 的 self.view 计算并设置您的 (x, y) 用于弹出框,这种方式受到一致的东西的限制。至于#2,特定文本在 UITextView 中的位置,可能类似于 stackoverflow.com/questions/28468187/…?如果没有更多细节,很难判断是什么导致了这些问题。
    • 我很好奇 meaningText.framepointOfTap 的 x、y 值是什么,如果您要在 debugPrint 所在的位置打印它们。
    • meaningText.frame.minX = 67.0 meaningText.frame.minY = 344.0 tappedWord = (172.5, 35.0)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-31
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多