2012-06-19 44 views
5

我有分類的UIWebView,以便我可以獲得觸摸事件並且還實現了這個方便的方法。我很好奇,如果這可以在實際的iOS設備上工作。我不在辦公室,所以我不知道是否。它似乎在模擬器中工作。tapAtPoint在UIWebView子類

- (void) tapAtPoint:(CGPoint)point 
{ 
    id /*UIWebBrowserView*/ webBrowserView = nil; 
    id webViewInternal = nil; 
    object_getInstanceVariable(self, "_internal", (void **)&webViewInternal); 
    object_getInstanceVariable(webViewInternal, "browserView", (void **)&webBrowserView); 

    if (webBrowserView) { 
     [webBrowserView tapInteractionWithLocation:point]; 
    } 
} 

有沒有人試過類似的東西?我肯定早上發現,哈哈。

+0

是不是' 「_internal」'無證?它會得到Apple的批准嗎? –

回答

0

請嘗試此代碼,在這裏它的工作正常。

/* TapDetectingWindow.m */ 

#import "TapDetectingWindow.h" 
@implementation TapDetectingWindow 
@synthesize viewToObserve; 
@synthesize controllerThatObserves; 
- (id)initWithViewToObserver:(UIView *)view andDelegate:(id)delegate { 
    if(self == [super init]) { 
     self.viewToObserve = view; 
     self.controllerThatObserves = delegate; 
    } 
    return self; 
} 
- (void)dealloc { 
    [viewToObserve release]; 
    [super dealloc]; 
} 
- (void)forwardTap:(id)touch { 
    [controllerThatObserves userDidTapWebView:touch]; 
} 
- (void)sendEvent:(UIEvent *)event { 
    [super sendEvent:event]; 
    if (viewToObserve == nil || controllerThatObserves == nil) 
     return; 
    NSSet *touches = [event allTouches]; 
    if (touches.count != 1) 
     return; 
    UITouch *touch = touches.anyObject; 
    if (touch.phase != UITouchPhaseEnded) 
     return; 
    if ([touch.view isDescendantOfView:viewToObserve] == NO) 
     return; 
    CGPoint tapPoint = [touch locationInView:viewToObserve]; 
    NSLog(@"TapPoint = %f, %f", tapPoint.x, tapPoint.y); 
    NSArray *pointArray = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%f", tapPoint.x], 
    [NSString stringWithFormat:@"%f", tapPoint.y], nil]; 
    if (touch.tapCount == 1) { 
     [self performSelector:@selector(forwardTapwithObject:pointArray afterDelay:0.5]; 
    } 
    else if (touch.tapCount > 1) { 
     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(forwardTap  object:pointArray]; 
    } 
} 
@end 


/* WebViewController.h */ 

@interface WebViewController : UIViewController<TapDetectingWindowDelegate> { 
    IBOutlet UIWebView *mHtmlViewer; 
    TapDetectingWindow *mWindow; 
} 

/* WebViewController.m */ 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    mWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]; 
    mWindow.viewToObserve = mHtmlViewer; 
    mWindow.controllerThatObserves = self; 
} 

- (void)userDidTapWebView:(id)tapPoint 
{ 
    NSLog(@"TapPoint = %f, %f", tapPoint.x, tapPoint.y); 
} 

謝謝,讓我知道你是否面臨任何問題。

+0

是否可以使用雙擊?我認爲有一個函數(tapinteractionwithloaction),當我使用performselector,併發送CGPoint時,該點不被使用。 – OMGPOP

+0

touch.tapCount == 2並相應地執行選擇器 – sagarcool89

0

簡短回答:是的,我以同樣的方式嘗試了這種方式,並且它也適用於真實設備(使用iOS 6進行測試)。

ARC版本的方法:

- (void) tapAtPoint:(CGPoint)point 
{ 
    Ivar internalWebViewIvar = class_getInstanceVariable([self class], "_internal"); 
    id internalWebView = object_getIvar(self, internalWebViewIvar); 

    Ivar browserViewIvar = class_getInstanceVariable(object_getClass(internalWebView), "browserView"); 
    id browserView = object_getIvar(internalWebView, browserViewIvar); 

    if (browserView) { 
     [browserView performSelector:@selector(tapInteractionWithLocation:) withObject:[NSValue valueWithCGPoint:point]]; 
    } 
}