我将UIWebView子类化,以便可以获取触摸事件并实现此便捷方法。我很好奇,如果这可以在实际的iOS设备上运行。我不在办公室,所以不知道是否在办公室。它似乎可以在模拟器中工作。

- (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];
    }
}

有没有人尝试过这样的事情?我肯定是早上发现的,哈哈。

最佳答案

请尝试此代码,在这里工作正常。

/* 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);
}

谢谢,如果您遇到任何问题,请告诉我。

10-08 06:07