`

UIWEBVIEW 处理Touch事件

    博客分类:
  • ios
 
阅读更多

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

#import <UIKit/UIKit.h>
@protocol TapDetectingWindowDelegate
- (void)userDidTapWebView:(id)tapPoint;
@end
@interface TapDetectingWindow : UIWindow {
    UIView *viewToObserve;
    id <TapDetectingWindowDelegate> controllerThatObserves;
}
@property (nonatomic, retain) UIView *viewToObserve;
@property (nonatomic, assign) id <TapDetectingWindowDelegate> controllerThatObserves;
@end
 Note 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"

@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

@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/

分享到:
评论

相关推荐

    oc中UIWebView交互 JS交互

    2. UIWebViewDelegate:设置UIWebView的代理,可以监听到JavaScript的某些事件,比如`shouldStartLoadWithRequest`方法,可以捕获到JavaScript发起的URL请求。 三、JavaScript与Objective-C/Cocoa Touch交互 1. ...

    UIWebView使用[归纳].pdf

    在iOS应用开发中,UIWebView是一个...Xcode是苹果官方提供的开发工具,支持Objective-C和Swift编程语言,包含了Cocoa Touch框架,用于构建iOS应用。通过Xcode,开发者可以轻松创建全屏内置浏览器,并实现上述各种功能。

    WebKit和Chromium源码及原理剖析.pdf

    - `EventHandler`负责实际的事件处理逻辑,包括触发事件、传递事件等。 #### 第23篇 iOS的QuickTime Plugin - **QuickTime Plugin概述**: - QuickTime Plugin是iOS中用于播放多媒体内容的一个插件。 - 本文...

    IOS应用源码——robin-cocoa-web-resource-8f47f25.rar

    1. **Cocoa Touch框架**:掌握如何使用UIKit来创建视图和控制器,以及如何实现触摸事件处理和动画效果。 2. **WKWebView的使用**:了解如何配置和使用WKWebView来显示网页,处理JavaScript交互,以及如何设置安全...

    CocoaBugs:在 Apple 的 Cocoa Cocoa Touch 框架中发现的错误

    3. **事件处理**:熟悉NSNotificationCenter和代理模式,确保事件正确传递。 4. **视图层次**:掌握如何有效管理视图层级,减少不必要的渲染和更新。 5. **KVC和KVO**:学习何时及如何使用,避免副作用和意外修改。 ...

    (0049)-iOS/iPhone/iPAD/iPod源代码-网页(Webview)-Web View Controller

    这个压缩包文件"(0049)-iOS/iPhone/iPAD/iPod源代码-网页(Webview)-Web View Controller"包含了一个实现网页浏览功能的示例项目,适用于iPhone、iPad以及iPod touch设备。在Mac环境下解压后,开发者可以深入...

    IOS应用源码之iPhone Browser-1.zip

    在iOS中,应用通常基于Cocoa Touch框架构建,它提供了UI元素和事件处理的基础。在这个项目中,我们可以预期源码会包含Objective-C或Swift语言编写的代码,用于实现浏览器功能。 1. **项目结构**: - `AppDelegate`...

    coretext swift写的富文本的实现

    - CoreText是一个Cocoa Touch框架,用于在iOS和macOS上处理文字排版和图形渲染。 - 它提供了对Unicode字符集的支持,可以处理多种语言和特殊字符。 - CoreText使用CTFontRef和CTParagraphStyleRef等结构体来定义...

    IOS应用源码之在text View上可进行html样式任意文字排列的组件.zip

    尽管TextView通常不支持HTML交互,但这个组件可能通过添加手势识别器或者监听触摸事件,使得用户能与HTML内容进行交互,例如点击链接或者图片。 8. **安全考虑** 当处理用户输入的HTML内容时,必须注意防止XSS...

    ios-IOS 代理、通知、block模式.zip

    Block可以捕获并存储其所在作用域内的变量,这使得Block非常适合用于异步操作的回调或者简单的事件处理。例如,使用gcd(Grand Central Dispatch)的dispatch_async方法进行异步任务,或者使用UIWebView的...

    仿新闻客服端详细页面的滑动效果

    首先,我们要明白滑动效果的核心在于处理用户的触摸事件(touch events)。在iOS平台上,我们可以使用UIScrollView或UIWebView来实现页面的滚动功能。而在Android中,我们通常会使用ScrollView或NestedScrollView,...

    previewDemo

    `UIKit`提供了`UIWebView`或`WKWebView`来展示PDF内容,而`CoreGraphics`则提供了低级别的图形接口,可以直接处理PDF文件的加载和渲染。 1. **UIWebView与WKWebView**: - `UIWebView`:旧版的网页视图控制器,...

    iPhone开发图书《iPhoneOpe》

    《iPhoneOpe》将深入探讨Cocoa Touch中的关键概念,如UIViewController、UIWebView、UITableView等,这些都是构建iPhone应用界面不可或缺的部分。此外,还会讲解手势识别、动画效果和多任务处理,使开发者能够创建出...

    text文本阅读器.zipIOS应用例子源码下载

    5. **用户交互**:iOS提供了丰富的手势识别和事件处理机制。例如,滑动翻页、捏合缩放等手势可以增强用户体验。`UIPanGestureRecognizer`、`UIPinchGestureRecognizer`等类可以实现这些功能。 6. **版本控制与更新*...

    iPhone Open Application Development

    3. **Cocoa Touch框架**:Cocoa Touch是iOS应用程序的基础,包含了一系列用于构建用户界面和处理事件的类。书中将详细解释各个关键组件如UIViewController、UIWebView、UITableView等的使用方法。 4. **iOS架构与...

    ios新浪微博项目视频教程:OAthu认证

    在iOS项目中,你需要在Xcode中创建一个新的Cocoa Touch Class,用于处理OAuth相关的网络请求。这个类通常会继承自`UIViewController`或`UITableViewController`,并包含一个`UIWebView`用于显示授权页面。在初始化这...

    8.网络篇1

    13. **Cocoa Touch**:Cocoa Touch是iOS应用开发的基础框架,包含了UI组件、触摸事件处理、动画等,如OpenAL(音频处理)、Bonjour(发现网络服务)、WebKit(网页渲染)和BSD Sockets(网络编程接口)都是在其上...

    《Programming iOS 4》源代码

    它提供了创建用户界面、处理事件、网络通信、多媒体支持等功能。在源代码中,你可以看到如何使用UIViewController、UITableView、UIButton等控件构建应用程序的界面。 3. **Xcode**: Apple的集成开发环境(IDE),...

    Beginning iPhone Development Exploring the iPhone SDK Example Code

    2. **Cocoa Touch框架**: Cocoa Touch是iOS应用的UI构建基石,包括UIKit、Foundation等子框架。书中会讲解如何使用UIViewController管理屏幕内容,UIWebView加载网页,UITableView展示列表,以及UIButton、UILabel、...

    一个iPhone项目的源代码

    - 项目基于Apple的iOS SDK,其中包括了Xcode IDE和Cocoa Touch框架,它们提供了构建用户界面和处理系统服务的工具和API。 2. **UI设计与交互**: - `Classes`目录可能包含了自定义的视图控制器,用于实现不同的...

Global site tag (gtag.js) - Google Analytics