Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1071460
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-13 17:29:36

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

C代码 复制代码 收藏代码
  1. #import
  2. @protocol TapDetectingWindowDelegate
  3. - (void)userDidTapWebView:(id)tapPoint;
  4. @end
  5. @interface TapDetectingWindow : UIWindow {
  6. UIView *viewToObserve;
  7. id controllerThatObserves;
  8. }
  9. @property (nonatomic, retain) UIView *viewToObserve;
  10. @property (nonatomic, assign) id controllerThatObserves;
  11. @end
#import @protocol TapDetectingWindowDelegate- (void)userDidTapWebView:(id)tapPoint;@end@interface TapDetectingWindow : UIWindow { UIView *viewToObserve; id controllerThatObserves;}@property (nonatomic, retain) UIView *viewToObserve;@property (nonatomic, assign) id controllerThatObserves;@endNote that we have variables which tell us the UIView on which to detect the events and the controller that receives the event information. Now, implement this class in the following way

#import "TapDetectingWindow.h"

C代码 复制代码 收藏代码
  1. @implementation TapDetectingWindow
  2. @synthesize viewToObserve;
  3. @synthesize controllerThatObserves;
  4. - (id)initWithViewToObserver:(UIView *)view andDelegate:(id)delegate {
  5. if(self == [super init]) {
  6. self.viewToObserve = view;
  7. self.controllerThatObserves = delegate;
  8. }
  9. return self;
  10. }
  11. - (void)dealloc {
  12. [viewToObserve release];
  13. [super dealloc];
  14. }
  15. - (void)forwardTap:(id)touch {
  16. [controllerThatObserves userDidTapWebView:touch];
  17. }
  18. - (void)sendEvent:(UIEvent *)event {
  19. [super sendEvent:event];
  20. if (viewToObserve == nil || controllerThatObserves == nil)
  21. return;
  22. NSSet *touches = [event allTouches];
  23. if (touches.count != 1)
  24. return;
  25. UITouch *touch = touches.anyObject;
  26. if (touch.phase != UITouchPhaseEnded)
  27. return;
  28. if ([touch.view isDescendantOfView:viewToObserve] == NO)
  29. return;
  30. CGPoint tapPoint = [touch locationInView:viewToObserve];
  31. NSLog(@"TapPoint = %f, %f", tapPoint.x, tapPoint.y);
  32. NSArray *pointArray = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%f", tapPoint.x],
  33. [NSString stringWithFormat:@"%f", tapPoint.y], nil];
  34. if (touch.tapCount == 1) {
  35. [self performSelector:@selector(forwardTap:) withObject:pointArray afterDelay:0.5];
  36. }
  37. else if (touch.tapCount > 1) {
  38. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(forwardTap:) object:pointArray];
  39. }
  40. }
  41. @end
@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(forwardTap:) withObject:pointArray afterDelay:0.5]; } else if (touch.tapCount > 1) { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(forwardTap:) object:pointArray]; }}@end

Implement the sendEvent method in the above way, and then you can send back the information you want back to the controller. There are few things that one needs to keep in mind. Make sure in your MainWindow.xib file, the window is of type TapDetectingWindow and not UIWindow. Only then all the events will pass through the above re-implemented sendEvent method. Also, make sure you call [super sendEvent:event] first and then do whatever you want. Now, you can create your UIWebView in the controller class in the following way

C代码 复制代码 收藏代码
  1. @interface WebViewController : UIViewController {
  2. IBOutlet UIWebView *mHtmlViewer;
  3. TapDetectingWindow *mWindow;
  4. }
  5. - (void)viewDidLoad {
  6. [super viewDidLoad];
  7. mWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0];
  8. mWindow.viewToObserve = mHtmlViewer;
  9. mWindow.controllerThatObserves = self;
  10. }
@interface WebViewController : UIViewController { 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.

转自:

阅读(629) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~