我有一个解决方案,但我认为有一个更好的方法来做到这一点。
我创建了一个TouchableWebView,它继承自UIWebView并保存方法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;
}
这不好,因为的文档说,你不应该继承它。我想有更好的方法,但这确实有效。你找到另一种解决方案吗?