4

我希望这个问题不是一个愚蠢的问题,但是如何将 UIPopoverController 视图定位在 UIWebView 上,以便弹出视图箭头指向单击以显示它的 UIWebView 链接?

我正在使用委托方法;

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] absoluteString] hasPrefix:@"myscheme:"] ) {
//UIPopoverController stuff here
return NO;
}

}

捕获和路由点击,但我不确定如何获取链接坐标来定位弹出视图。

非常感谢任何帮助或指向相关信息的指针。

4

3 回答 3

7

我有一个可行的解决方案,但我认为有更好的方法来做到这一点。

我创建了一个继承自 UIWebView 的 TouchableWebView,并将触摸位置保存在方法 hitTest 中。之后,您需要为您的 webview 设置一个委托并实现该方法-webView:shouldStartLoadWithRequest:navigationType:

TouchableWebView.h:

@interface TouchableWebView : UIWebView {
CGPoint lastTouchPosition;
}
@property (nonatomic, assign) CGPoint lastTouchPosition;

TouchableWebView.m:

// I need to retrieve the touch position in order to position the popover
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    self.lastTouchPosition = point;
    return [super hitTest:point withEvent:event];
}

在 RootViewController.m 中:

[self.touchableWebView setDelegate:self];

// WebView delegate, when hyperlink clicked
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request     navigationType:(UIWebViewNavigationType)navigationType {

    if (navigationType == UIWebViewNavigationTypeLinkClicked) 
    {   
        CGPoint touchPosition = [self.view convertPoint:self.touchableWebView.lastTouchPosition fromView:self.touchableWebView];
        [self displayPopOver:request atPosition:touchPosition isOnCelebrityFrame:FALSE];
        return FALSE;
    }

    return TRUE;
}

这不好,因为文档UIWebView说你不应该子类化它。我想有一个更好的方法,但这确实有效。你找到另一个解决方案了吗?

于 2010-07-30T21:34:42.873 回答
2

给每个链接一个唯一的 ID 并通过你的“myscheme:”逻辑将该 ID 传递给你的 Obj-C 代码。

然后,您可以通过 Javascript 调用确定 UIWebView 中链接的左侧和顶部偏移量:

NSString *leftJS = [NSString stringWithFormat:@"document.getElementById('%d').offsetLeft - scrollX", linkID];
NSInteger leftOffset = [[currentTextView stringByEvaluatingJavaScriptFromString:js] intValue];

NSString *topJS = [NSString stringWithFormat:@"document.getElementById('%d').offsetTop - scrollY", linkID];
NSInteger topOffset = [[currentTextView stringByEvaluatingJavaScriptFromString:js] intValue];

减去 scrollX/scrollY 说明用户向左或向下滚动了多少 UIWebView。

我的理解是 offsetLeft 和 offsetTop 返回元素在其父元素中的偏移量。因此,希望您的链接位于层次结构的顶部,而不是嵌入 div 中,否则确定偏移量会变得更加复杂。

于 2011-06-17T21:20:07.727 回答
1

对我来说,使用getBoundingClientRect()并将其转换为CGRect效果很好。

NSString *js = [NSString stringWithFormat:@"JSON.stringify($(\"a[href='%@']\")[0].getBoundingClientRect())", [inRequest URL]];                                                                          
// {top:, right:, bottom:, left:, width:, height:}
NSString *rectJson =  [webView stringByEvaluatingJavaScriptFromString:js];                                                                                                                       
NSError *error = nil;                                                                                                                                                                            
NSDictionary *rectDict = [NSJSONSerialization JSONObjectWithData:[rectJson dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];                         
if (error) {
    NSLog(@"json deserialization failed: %@\n%@", rectJson, error);                                                                                                                              
}
CGRect rect = CGRectMake([rectDict[@"left"] floatValue], [rectDict[@"top"] floatValue], [rectDict[@"width"] floatValue], [rectDict[@"height"] floatValue]);                                      
于 2015-11-18T08:21:32.123 回答