views:

360

answers:

2

Setup: I have two views that I need to respond to the touch event, and they are layered out on top of one another. View 1 is on top of View 2. View 2 is a UIWebView. View 1 is sublclassed to capture the touch event.

My problem is that if I try to call the UIWebView event handlers (touchesBegan: and touchesEnded:) from within the event handlers of View 1, which is the first responder, nothing happens. However if I set View 1 to userInteractionEnabled = NO, then the touch goes through that view and is processed properly by the 2nd view.

Any ideas on how I can have 2 views respond to a touch event? Unfortunately the 2nd view is a UIWebView, so I need to actually call the event handler and not a different method, etc...

Thanks in advance for any advice, Joel

A: 

I have the same problem as yours... Did you found the solution ?

I tried

  • window
    • surveyViewController
    • tabBarController wich contains viewController like map, web and customs with buttons etc...

My "surveyController" have a surveyedViewController that allow me to do

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

//FOR EACH FINGERS
for (UITouch *touch in [touches allObjects]) {

    //GET THE FINGER LOCATION ON THE SCREEN
    CGPoint location = [touch locationInView:surveyedViewController.view];

    //REPORT THE TOUCH
    NSLog(@"[%@] touchesBegan (%i,%i)",  [surveyedViewController class],(NSInteger) location.x, (NSInteger) location.y);

    //SEND TOUCH TO THE SURVEYED viewController
    [surveyedViewController touchesBegan:touches withEvent:event];  

    }
}

I hope that you have the solution and thank you !

PJ.
A: 

Here is the solution to my problem. It's working on all kinds of UIView ! If someone whant to improve this code on the

catchUIEventTypeMotion default: ...

I hope that this code will be helpful for you.

PJ.

CustomWindow.h

#import <Foundation/Foundation.h>

@interface CustomWindow : UIWindow {
}

- (void) sendEvent:(UIEvent *)event;

@end

CustomWindow.m

#import "CustomWindow.h"

@implementation CustomWindow

- (void) sendEvent:(UIEvent *)event
{       
    switch ([event type])
    {
        case UIEventTypeMotion:
            NSLog(@"UIEventTypeMotion");
            [self catchUIEventTypeMotion: event];
            break;

        case UIEventTypeTouches:
            NSLog(@"UIEventTypeTouches");
            [self catchUIEventTypeTouches: event];
            break;      

        default:
            break;
    }
    /*IMPORTANT*/[super sendEvent:(UIEvent *)event];/*IMPORTANT*/
}

- (void) catchUIEventTypeTouches: (UIEvent *)event
{
    for (UITouch *touch in [event allTouches])
    {
        switch ([touch phase])
        {
            case UITouchPhaseBegan:
                NSLog(@"UITouchPhaseBegan");
                break;

            case UITouchPhaseMoved:
                NSLog(@"UITouchPhaseMoved");
                break;

            case UITouchPhaseEnded:
                NSLog(@"UITouchPhaseEnded");
                break;

            case UITouchPhaseStationary:
                NSLog(@"UITouchPhaseStationary");
                break;

            case UITouchPhaseCancelled:
                NSLog(@"UITouchPhaseCancelled");
                break;

            default:
                NSLog(@"iPhone touched");
                break;
        }
    }
}

- (void) catchUIEventTypeMotion: (UIEvent *)event
{
    switch ([event subtype]) {
        case UIEventSubtypeMotionShake:
            NSLog(@"UIEventSubtypeMotionShake");
            break;

        default:
            NSLog(@"iPhone in movement");
            break;
    }
}

@end

AppDelegate.h

#import <UIKit/UIKit.h>
#import "CustomWindow.h"

@interface AppDelegate : NSObject <UIApplicationDelegate>
{
    CustomWindow *window;
}

@property (nonatomic, retain) IBOutlet CustomWindow *window;

@end
PJ.