Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-07-2010, 06:49 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
borg359 is on a distinguished road
Default Detecting taps and events on UIWebView

Hi guys,
I'm in the process of trying to implement a method to detect taps on a UIWebView in order to hide the navigation bar on demand. To do this, I've been following the often cited example found at the link below:

Detecting taps and events on UIWebView ? The right*way - The Spoken Word

This problem is that I'm not entirely sure how to implement their example. I've created a UIWindow sub-class as they suggested but I'm not sure how to create the final UIWebView in the controller class. I've tried placing

Code:
@interface WebViewController : UIViewController<TapDetectingWindowDelegate> {
Inside a custom viewcontroller only to get the following error

Code:
error: no super class declared in @interface for 'TapDetectingWindowController'
Can anyone whose actually made this work describe how this is to be done? Many thanks!


-dan
borg359 is offline   Reply With Quote
Old 11-08-2010, 05:57 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 60
ZubiZubi is on a distinguished road
Default

have you included both Tapxxxxx.h and .m files in your project ?

btw, Tapxxxx.h/m files alone are not enough to implement hide/show like in iPhone Photo application. You need to combine tap detecting code (i.e. Tapxxx code you mentioned above) with fading code found in this thread

http://www.iphonedevsdk.com/forum/ip...plication.html

yes, I have successfully implemented in my app "500 Mobile Sites" but it took many iterations and quite some time to get it working.
__________________
My Apps in AppStore
ZubiZubi is offline   Reply With Quote
Old 11-08-2010, 01:50 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
borg359 is on a distinguished road
Default

Here is what I did to get this to work.

First, create a two new files, TapDetectingWindow.h and TapDetectingWindow.m as follows:

Code:
//
//  TapDetectingWindow.h
//  

#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
and

Code:
//
//  TapDetectingWindow.m
//  

#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];
    }
}
Next, add a reference to import the TapDetectingWindow.h file in the header file of view controller in which you would like to animate/hide the navigation bar

Code:
#import "TapDetectingWindow.h"
Now change the default @interface line in the same file to make TapDetectingWindow the delegate


Code:
@interface <Your View Controller Name Here> : UIViewController<TapDetectingWindowDelegate> {
Now add a global instance for the TapDetectingWindow object in the same file

Code:
TapDetectingWindow *mWindow;
Finally, define a new public method in the same file that will allow you make things happen when the user taps on your view

Code:
-(void)userDidTapWebView:(id)touch;
Now, in the method file of your view controller, add a viewDidLoad method if it doesn't exist, or add the following lines to your existing method if it does exist. In my example, the view for which I want to the navbar to hide/show is called mainView. Change this to whatever your view is named.

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
    mWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0];
    mWindow.viewToObserve = mainView;
    mWindow.controllerThatObserves = self;
}
In the same file, create a new method to handle the user taps. In the example below, I hide the nav bar when the user taps the screen and then show it again when they tap again. I store the state of the navbar in a global boolean called NavBarHidden. When NavBarHidden == NO, the navbar is hidden and vice versa. Be sure to define BOOL * NaVBarHidden; in your header file for this work. I've also included code to hide/show a bottom tool bar and the iphone status bar.

Code:
- (void)userDidTapWebView:(id)touch {
	
	if (NaVBarHidden == NO) {
		
		// Hide the navigation bar
		[self.navigationController setNavigationBarHidden:YES animated:YES];	

		// Hide the tool bar
		[self.navigationController setToolbarHidden:YES animated:YES];
		
                // Hide the status bar
//		[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];

		// Record the change	
		NaVBarHidden = YES;
		
	} else {
		
		// Show the navigation bar
		[self.navigationController setNavigationBarHidden:NO animated:YES];
		
		// Show the tool bar
		[self.navigationController setToolbarHidden:NO animated:YES];
		
                // Show the status bar
//		[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];

		// Record the change
		NaVBarHidden = NO;
		
	}
	
	NSLog(@"The value of the bool is %@\n", (NaVBarHidden ? @"YES" : @"NO"));
	
}
If you would like your view to fill the entire screen beneath your navigation controller, be sure to add the following lines to your -(void)loadView method

Code:
self.navigationController.navigationBar.translucent = YES;
self.navigationController.toolbar.translucent = YES;
Last but not least, open your MainWindow.xib file in IB and be sure to change the window of type UIWindow to TapDetectingWindow. None of this will work until that is done.

Good luck!
borg359 is offline   Reply With Quote
Reply

Bookmarks

Tags
events, navigationbar, taps, uiwebview, webviewcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 342
10 members and 332 guests
7twenty7, chiataytuday, condor304, Creativ, Domele, dreamdash3, laureix68, LEARN2MAKE, mistergreen2011, Paul Slocum
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,660
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, laureix68
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:26 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0