2011-06-25
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");
}
}
}
トラックバック - http://d.hatena.ne.jp/Kmusiclife/20110625/1309013026
リンク元
- 1 http://twitter.com/
- 1 http://www.google.co.jp/
- 1 http://www.google.co.jp/search?sourceid=chrome&ie=UTF-8&q=-+(void)touchesBegan:(NSSet+*)touches+withEvent:(UIEvent+*)event
- 1 http://www.google.co.jp/url?sa=t&source=web&cd=6&ved=0CEQQFjAF&url=http://d.hatena.ne.jp/Kmusiclife/20110620&rct=j&q=nsindexpath insertRowsAtIndexPaths&ei=YAwGTqLPIeOJmQWVoezWDQ&usg=AFQjCNEZI4XZlCl6qHpgjCicVrFWfp6oWg
