2010-03-04 59 views
0

安装程序: 我有两个视图需要响应触摸事件,并且它们彼此层叠在一起。 视图1位于视图2的顶部。视图2是一个UIWebView。视图1被隐藏以捕捉触摸事件。需要多个视图来响应iPhone应用程序中的触摸事件

我的问题是,如果我尝试从第一响应者视图1的事件处理程序中调用UIWebView事件处理程序(touchesBegan:和touchesEnded :),则不会发生任何事情。但是,如果我将视图1设置为userInteractionEnabled = NO,那么触摸将通过该视图并由第二视图正确处理。

关于如何让2个视图响应触摸事件的任何想法?不幸的是,第二个观点是一个UIWebView,所以我需要实际调用事件处理程序,而不是一个不同的方法,等等。

预先感谢任何建议, 乔尔

+0

我想接下来的事情是增加上述视图1和视图一个UIView 2,在那里捕捉触摸事件,然后转发它到其他每个视图。视图1响应得很好,但我无法获得UIWebView(视图2)响应转发的事件。 任何人都有任何想法为什么UIWebView不想玩好? 谢谢。 – Joel 2010-03-04 18:26:27

回答

1

这里是解决我的问题。它正在处理各种UIView!如果有人想改善这个代码,我希望这段代码对你有所帮助。

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 
相关问题