这是我的问题的解决方案。它适用于各种 UIView !如果有人想在
catchUIEventTypeMotion default: ...
我希望这段代码对你有所帮助。
PJ.
自定义窗口.h
#import <Foundation/Foundation.h>
@interface CustomWindow : UIWindow {
}
- (void) sendEvent:(UIEvent *)event;
@end
自定义窗口.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