我有一个可行的解决方案,但我认为有更好的方法来做到这一点。
我创建了一个继承自 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
说你不应该子类化它。我想有一个更好的方法,但这确实有效。你找到另一个解决方案了吗?