私はそれをどうしたらいいですか?のサブクラスを作成します。このサブクラスの目的は、touchesBegan:withEvent:
メソッドで受信した最初と最後のUITouch
オブジェクトを覚えておくことです。それ以外のものはに転送されます。
認識機能がトリガーされると、認識機能はsender
パラメータとして渡されます。最初と最後のタッチオブジェクトを求めてから、locationInView:
メソッドとtimestamp
プロパティを使用して、スワイプの速度(速度=距離の変化/時間の変化)を調べることができます。その後、別の場所で
@interface DDSwipeGestureRecognizer : UISwipeGestureRecognizer
@property (nonatomic, retain) UITouch * firstTouch;
@property (nonatomic, retain) UITouch * lastTouch;
@end
@implementation DDSwipeGestureRecognizer
@synthesize firstTouch, lastTouch;
- (void) dealloc {
[firstTouch release];
[lastTouch release];
[super dealloc];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self setFirstTouch:[touches anyObject]];
[super touchesBegan:touches withEvent:event];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self setLastTouch:[touches anyObject]];
[super touchesEnded:touches withEvent:event];
}
@end
をあなたがしたい:
は、だから、このようなものになるだろう
DDSwipeGestureRecognizer *swipe = [[DDSwipeGestureRecognizer alloc] init];
[swipe setTarget:self];
[swipe setAction:@selector(swiped:)];
[myView addGestureRecognizer:swipe];
[swipe release];
そして、あなたの行動は何かのようになります:
- (void) swiped:(DDSwipeGestureRecognizer *)recognizer {
CGPoint firstPoint = [[recognizer firstTouch] locationInView:myView];
CGPoint lastPoint = [[recognizer lastTouch] locationInView:myView];
CGFloat distance = ...; // the distance between firstPoint and lastPoint
NSTimeInterval elapsedTime = [[recognizer lastTouch] timestamp] - [[recognizer firstTouch] timestamp];
CGFloat velocity = distance/elapsedTime;
NSLog(@"the velocity of the swipe was %f points per second", velocity);
}
警告:コードはブラウザに入力され、コンパイルされません。警告実装者。
私はここにおり、私は非常に感謝している皆のコメントと回答を統合しようとしています。あなたの前のコメントについては、私は投票をして、助けてくれた人に何かを返すことができるように、私はちょっとしています。 – Mytheral