gpt4 book ai didi

objective-c - UIWebView 子类上的 tapAtPoint

转载 作者:可可西里 更新时间:2023-11-01 06:16:41 26 4
gpt4 key购买 nike

我已经将 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);
}

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

关于objective-c - UIWebView 子类上的 tapAtPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11093431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com