我创建的一个子类UIWebView
,并且已经实施了
touchesBegan
,touchesMoved
和touchesEnded
方法.
但是webview子类不处理touch
事件.
是否有任何方法来处理UIWebView
子类内的触摸事件???
不需要子类化,只需添加一个UITapGestureRecognizer
:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMethod)]; [tap setNumberOfTapsRequired:1]; // Set your own number here [tap setDelegate:self]; // Add theprotocol [self.myWebView addGestureRecognizer:tap];
在头文件中添加协议,并添加此方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }
如果您只需要处理手势,同时保持UIWebView的其余功能不变,您可以继承UIWebView并使用此策略:
在UIWebView子类的init方法中,添加手势识别器,例如:
UISwipeGestureRecognizer * swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGestureRightMethod)]; swipeRight.direction = UISwipeGestureRecognizerDirectionRight; [self addGestureRecognizer:swipeRight]; swipeRight.delegate = self;
然后,将此方法添加到您的类:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
添加并处理指定的选择器到类,在本例中为"handleSwipeGestureRightMethod",你很高兴...
你可以把一个UIView
在你的UIWebView
,并在此改变touchesDidBegin
等,然后将它们发送到您的网页流量.例如:
用户接触你UIView
,这会激起你的兴趣
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // Execute your code then send a touchesBegan to your webview like so: [webView touchesBegan:touches withEvent:event]; return; }
你UIView
必须通过webview.
我不确定这是不是你想要的(这不是你要求的,但它可能会取决于你的最终游戏是什么),但你可以从UIWebView内部解释JavaScript的触摸,并获得javascript到做
document.location='http://null/'+xCoord+'/'+yCoord; // Null is arbitrary.
然后,您可以使用UIWebView的委托方法捕获它
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
如果request.URL.host(或其他任何东西)isEqualToString:@"null"采取相关的操作(并返回NO而不是YES).您甚至可以通过执行以下操作将JS添加到每个页面:
- (void)webViewDidFinishLoad:(UIWebView *)webView { [webView stringByEvaluatingJavaScriptFromString:@"window.ontouchstart=function(/* ... */);"]; }
希望这可以帮助?