UIViewでUITouchを使って右フリック・左フリック・上フリック・下フリックを検出する
UIViewControllerを利用した上で右フリック・左フリック・上フリック・下フリックを検出する方法です。
UIViewControllerのdelegateで実装される
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
を利用します。
フリックのスタートとエンドを受け取ってそれぞれの座標を比較してフリックの方向を判断しています。
.hファイル
@interface classname : UIViewController { CGPoint _tBegan, _tEnded; } @end
.mファイル
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch * touchBegan = [touches anyObject]; _tBegan = [ touchBegan locationInView: self.view ]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch * touchEnded = [touches anyObject]; _tEnded = [ touchEnded locationInView: self.view ]; NSInteger distanceHorizontal = ABS( _tEnded.x - _tBegan.x ); NSInteger distanceVertical = ABS( _tEnded.y - _tBegan.y ); NSLog(@"%d", distanceHorizontal); NSLog(@"%d", distanceVertical); if ( distanceHorizontal > distanceVertical ) { if ( _tEnded.x > _tBegan.x ) { NSLog(@"RIGHT Flick"); } else { NSLog(@"LEFT Flick"); } } else { if ( _tEnded.y > _tBegan.y ) { NSLog(@"DOWN Flick"); } else { NSLog(@"UP Flick"); } } }