xcode4.5.2
スワイプの処理サンプルです。
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { NSDate *startTime; CGPoint startPt; int swipe_direction; } @property (nonatomic, retain) NSDate *startTime; @end
ViewController.m
#import "ViewController.h" #define HORIZ_SWIPE_MIN 12 #define VERT_SWIPE_MAX 8 #define SWIPE_NON 0 #define SWIPE_LEFT 1 #define SWIPE_RIGHT 2 @interface ViewController () @end @implementation ViewController @synthesize startTime; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } // タッチ開始 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { startPt = [[touches anyObject] locationInView:self.view]; startTime = [[NSDate date] retain]; swipe_direction = SWIPE_NON; } // タッチ移動 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint curPt = [[touches anyObject] locationInView:self.view]; // 水平 if (fabsf(startPt.x - curPt.x) >= HORIZ_SWIPE_MIN && fabsf(startPt.y - curPt.y) <= VERT_SWIPE_MAX) { if (startPt.x < curPt.x) { // 左にスワイプ swipe_direction = SWIPE_LEFT; } else { // 右にスワイプ swipe_direction = SWIPE_RIGHT; } } } // タッチ終了 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint curPt = [[touches anyObject] locationInView:self.view]; if (swipe_direction != SWIPE_NON) { // スワイプの状態 // 動いた時間 NSTimeInterval dt = -[startTime timeIntervalSinceNow]; // 移動距離 int dx = fabsf(startPt.x - curPt.x); // 移動スピード double speed = dx / dt; NSLog(@"dir:%i, dx:%i, dt:%f, speed:%f",swipe_direction, dx, dt, speed); } } @end
右にスワイプは2、左にスワイプは1とでます。
Via:[iOS SDK]UIViewでスワイプを検出する方法
Via:[PDF]iOSイベント処理ガイド (TP40009541 1.5) – Apple Developer
日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)