![](/img/trans.png)
[英]display iPhone-View in a UIPopoverController on iPad (iPhone SDK)
[英]iPad UIWebView position a UIPopoverController view at a href link coords
我希望這個問題不是一個愚蠢的問題,但是如何在UIWebView上定位UIPopoverController視圖,以便彈出視圖箭頭指向被點擊以顯示它的UIWebView鏈接?
我正在使用委托方法;
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] absoluteString] hasPrefix:@"myscheme:"] ) {
//UIPopoverController stuff here
return NO;
}
}
捕獲和路由點擊但我不確定如何獲得鏈接坐標定位彈出視圖。
任何幫助或指向相關信息的指針都將非常感激。
我有一個有效的解決方案,但我認為有更好的方法。
我創建了一個從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
文檔說你不應該將它子類化。 我想有一個更好的方法,但這確實有效。 你找到了另一種解決方案嗎
為每個鏈接提供一個唯一的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中,否則確定偏移變得更復雜。
對我來說,使用getBoundingClientRect()
並將其轉換為CGRect
非常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]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.