指向弹出窗口指向在文本视图中点击的单词

时间:2018-04-05 12:46:34

标签: ios iphone popup position

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

以下是一些代码:

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

Screenshot on iPhone 6s

1 个答案:

答案 0 :(得分:0)

不确定这些是否适用:

如果您使用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元素后,您可以从那里计算弹出位置。