iOS   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – 如何在UIWebview中获取Hyperlink的坐标大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在UIWebview中加载pdf(具有多个超链接)文档.我必须动态地在超链接上显示UIPopover. 我可以使用TapGesture Action方法捕获超链接的坐标 - (void)tapAction:(UITapGestureRecognizer *)sender { self.point = [sender LOCATIOnInView:self.myWebView];
我正在UIWebview中加载pdf(具有多个超链接)文档.我必须动态地在超链接显示UIPopover.

我可以使用TapGesture Action方法捕获超链接的坐标

- (void)tapAction:(UITapGestureRecognizer *)sender    
{
    self.point = [sender LOCATIOnInView:self.myWebView];  
}

并使用以下方法在超链接显示UIPopover

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithrequest:(NSURLrequest *)request navigationType:(UIWebViewNavigationTypE)navigationType
{
    NSURL *rqsturl = [request URL];
    if (([[rqsturl scheR_821_11845@e] isEqualToString: @"https"] || [[rqsturl scheR_821_11845@e] isEqualToString: @"http"]) && (navigationType == UIWebViewNavigationTypeLinkClicked))
    {
        [self.myWebView stopLoading];
        CGRect rect = CGRectMake(self.point.x,self.point.y-5,5,5);
        UIPopoverController *popController = [[UIPopoverController alloc] initWithContentViewController:contentViewController];
        popController.popoverContentSize = CGSizeMake(500,200);
        self.popController = popController;
        self.popController.delegate =self;
        UIPopoverArrowDirection direction = UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown;
        self.popController.popoverLayoutMargins = UIEdgeInsetsmake(0,rect.origin.x,1,1);
        [self.popController presentPopoverFromRect:rect inView:webView permittedArrowDirections:direction animated:YES];
    }
    return YES;
}

但问题是,如果我在1秒或2秒内在两个不同的位置点击,例如First Tap是On Hyperlink,而Second Tap是在“UIWebview中的其他地方”,UIPopover仅在第二个点击位置呈现,而不是在超链接位置.
我必须仅基于超链接位置显示UIPopover,而不是在其他位置.我如何解决此问题?

解决方法

使用叠加视图

>替换您的方法以通过点按覆盖来注册点击位置. UITapGestureRecognizer具有以下限制:

>当超级链接发生在超链接之外时,由于UITapGestureRecognizer,它会注册其位置.
>不幸的是,UIWebview超链接点击优先于手势识别器,你永远不会得到质心.这是真正的问题,导致弹出窗口错位.

> iOS 9中不推荐使用UIPopoverController.

> tapAction和shouldStartLoadWithrequest没有耦合,并且可以彼此独立地发生.而且,它们基本上是相互排斥的.

objective-c – 如何在UIWebview中获取Hyperlink的坐标

使用叠加层在该视图中注册位置,然后点击下方的视图.如果您的叠加层和Web视图具有相同的框架,则可以互换使用分接位置.叠加将保证紧密耦合,您的方法的其余部分将按设计工作.

class TapOverlayView: UIView {
    var centroid:CGRect = CGRect.zero

    override func hitTest(_ point: CGPoint,with event: UIEvent?) -> UIView? {
        centroid = CGRect(origin: point,size: CGSize(width: 1,height: 1))
        return nil // tap through
    }
}

代表

extension ViewController: UIWebViewDelegate {
    public func webView(_ webView: UIWebView,shouldStartLoadWith request: URLrequest,navigationType: UIWebViewNavigationTypE) -> Bool {
        let rqsturl = request.url

        if( rqsturl!.scheR_821_11845@e?.contains("http"))! && ( .linkClicked == navigationTypE) {
            webView.stopLoading()

            let contentViewController = storyboard!.instantiateViewController(withIdentifier: "popover")
            contentViewController.modalPresentationStyle = .popover
            contentViewController.preferredContentSize = CGSize(width: 200,height: 40)

            if let popController = contentViewController.popoverPresentationController {
                popController.permittedArrowDirections = .down
                popController.sourceView = webView
                popController.sourceRect = CGRect(origin: tap.centroid.origin,size: CGSize.zero)
                present(contentViewController,animated: true,completion: nil)
            }
        }
        return true
    }
}

►在GitHub找到此解决方案,并在Swift Recipes找到其他详细信息.

大佬总结

以上是大佬教程为你收集整理的objective-c – 如何在UIWebview中获取Hyperlink的坐标全部内容,希望文章能够帮你解决objective-c – 如何在UIWebview中获取Hyperlink的坐标所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。