touchesBeganなどUIResponderのメソッドをdelegateする
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesEnded: (NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
タッチイベントを受け取るメソッドUIResponderクラスのメソッドなどを他のクラスで処理したいことがあります。-(void) flickRight; のようなメソッドを書くだけで綺麗に右フリックしてくれるなど便利そうで。その時にきっと必要になるかと思います。
こちらはすごく参考になりました。
http://stackoverflow.com/questions/2003201/observing-pinch-multi-touch-gestures-in-a-uitableview
TouchesWindow.hファイル
#import <Foundation/Foundation.h> @protocol TouchesWindowDelegate - (BOOL)originalEvent:(UIEvent *)event; @end @interface TouchesWindow : UIWindow { id <TouchesWindowDelegate> delegate; } @property(nonatomic, assign) id <TouchesWindowDelegate> delagate; @end
TouchesWindow.mファイル
#import "TouchesWindow.h" @implementation TouchesWindow @synthesize delagate; - (void)sendEvent:(UIEvent *)event { NSLog(@"%@", event); [super sendEvent:event]; if(delegate == nil) return; UITouch * touch = [touches anyObject]; switch(touch.phase){ case UITouchPhaseBegan: NSLog(@"touches began event."); // [self.delegate originalEvent:event]; // delegate用 break; } } @end
ここからがポイントでMainWindow.xibでWindowに対して上記のUIWindowのサブクラスEventInterceptWindowと結びつけます。
MainWindow.xibから呼び出されたTouchesWindow、そのサブクラスであるUIWindowの(void)sendEvent:(UIEvent *)eventを上書きしてイベントを検出します。ここまで理解できてればあとは実装するだけ。
ViewControllerなどから
-(void)main{ TouchesWindow * touchesWindow = [[TouchesWindow alloc]init]; touches.delegate = self; } // TouchesWindowのdelegate - (BOOL)originalEvent:(UIEvent *)event{ // do something return YES; }