首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

UIWEBVIEW 处置Touch事件

2012-09-14 
UIWEBVIEW 处理Touch事件Recently, I was working on a project which required detection of tap and eve

UIWEBVIEW 处理Touch事件

Recently, I was working on a project which required detection of tap and events on the UIWebView. We wanted to find out the HTML element on which the user taps in the UIWebView and then depending on the element tapped some action was to be performed. After some Googling, I found out the most of the users lay a transparent UIView on top of the UIWebView, re-implement the touch methods of UIResponder class (Ex: -touchesBegan:withEvent:) and then pass the events to the UIWebView. This method is explained in detail?here. There are multiple problems with the method.

  1. Copy/Selection stops working on UIWebView
  2. We need to create a sub-class of UIWebView while Apple says we should not sub-class it.
  3. A lot other UIWebView features stop working.

We ultimately found out that the right way to implement this is by sub-classing UIWindow and re-implementing the -sendEvent: method. Here is how you can do it. First, create a UIWindow sub-class

@interface WebViewController : UIViewController<TapDetectingWindowDelegate> {    IBOutlet UIWebView *mHtmlViewer;     TapDetectingWindow *mWindow;}- (void)viewDidLoad {    [super viewDidLoad];    mWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0];    mWindow.viewToObserve = mHtmlViewer;    mWindow.controllerThatObserves = self;}

?Remember you'll need to write the method userDidTapWebView in your controller class. This is the method that is called in order to send the event information to the controller class. In our case above we are sending the point in the UIWebView at which the user tapped. Hope this was useful. Please let me know your suggestions and feedback.

?

转自:http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/

热点排行