我创建的一个子类UIWebView,并且已经实施了
touchesBegan,touchesMoved和touchesEnded方法.
但是webview子类不处理touch事件.
是否有任何方法来处理UIWebView子类内的触摸事件???
小智 46
不需要子类化,只需添加一个UITapGestureRecognizer:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapMethod)];
[tap setNumberOfTapsRequired:1]; // Set your own number here
[tap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol
[self.myWebView addGestureRecognizer:tap];
Run Code Online (Sandbox Code Playgroud)
<UIGestureRecognizerDelegate>在头文件中添加协议,并添加此方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
如果您只需要处理手势,同时保持UIWebView的其余功能不变,您可以继承UIWebView并使用此策略:
在UIWebView子类的init方法中,添加手势识别器,例如:
UISwipeGestureRecognizer * swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGestureRightMethod)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:swipeRight];
swipeRight.delegate = self;
Run Code Online (Sandbox Code Playgroud)
然后,将此方法添加到您的类:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
添加并处理指定的选择器到类,在本例中为"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;
}
Run Code Online (Sandbox Code Playgroud)
你UIView必须通过webview.
我不确定这是不是你想要的(这不是你要求的,但它可能会取决于你的最终游戏是什么),但你可以从UIWebView内部解释JavaScript的触摸,并获得javascript到做
document.location='http://null/'+xCoord+'/'+yCoord; // Null is arbitrary.
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用UIWebView的委托方法捕获它
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Run Code Online (Sandbox Code Playgroud)
如果request.URL.host(或其他任何东西)isEqualToString:@"null"采取相关的操作(并返回NO而不是YES).您甚至可以通过执行以下操作将JS添加到每个页面:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView stringByEvaluatingJavaScriptFromString:@"window.ontouchstart=function(/* ... */);"];
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助?
你的意思是当调用touchesBegan、touchesMoved和touchesEnded时你的子类实现不会被调用?
这听起来像是您创建对象实例的方式有问题。我认为需要更多细节。
(采取评论形式)
头文件
#import <UIKit/UIKit.h>
@interface MyWebView : UIWebView { } @end
Run Code Online (Sandbox Code Playgroud)
实施文件
#import "MyWebView.h"
@implementation MyWebView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) { } return self;
}
- (void)drawRect:(CGRect)rect {
NSLog(@"MyWebView is loaded");
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches began");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Touches ended");
}
- (void)dealloc {
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52321 次 |
| 最近记录: |