4

我将 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];
    }
}

有没有人尝试过这样的事情?我肯定会在早上发现,大声笑。

4

2 回答 2

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]];
    }
}
于 2013-06-21T09:57:24.117 回答
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);
}

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

于 2013-06-19T08:18:42.873 回答