距離を計算できるように、スワイプジェスチャまたはタッチの長さを取得する方法があるかどうか、考えはありますか?
33273 次
6 に答える
56
SwipeGesture は、ジェスチャが終了したときに、場所にアクセスできるメソッドを 1 回だけトリガーするため、スワイプ ジェスチャから距離を取得することは不可能です。
たぶん、UIPanGestureRecognizer を使いたいでしょう。
パン ジェスチャを使用できる場合は、パンの開始点を保存し、パンが終了した場合は距離を計算します。
- (void)panGesture:(UIPanGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
startLocation = [sender locationInView:self.view];
}
else if (sender.state == UIGestureRecognizerStateEnded) {
CGPoint stopLocation = [sender locationInView:self.view];
CGFloat dx = stopLocation.x - startLocation.x;
CGFloat dy = stopLocation.y - startLocation.y;
CGFloat distance = sqrt(dx*dx + dy*dy );
NSLog(@"Distance: %f", distance);
}
}
于 2011-01-28T14:32:03.667 に答える
16
スイフトで
override func viewDidLoad() {
super.viewDidLoad()
// add your pan recognizer to your desired view
let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panedView))
self.view.addGestureRecognizer(panRecognizer)
}
@objc func panedView(sender:UIPanGestureRecognizer){
var startLocation = CGPoint()
//UIGestureRecognizerState has been renamed to UIGestureRecognizer.State in Swift 4
if (sender.state == UIGestureRecognizer.State.began) {
startLocation = sender.location(in: self.view)
}
else if (sender.state == UIGestureRecognizer.State.ended) {
let stopLocation = sender.location(in: self.view)
let dx = stopLocation.x - startLocation.x;
let dy = stopLocation.y - startLocation.y;
let distance = sqrt(dx*dx + dy*dy );
NSLog("Distance: %f", distance);
if distance > 400 {
//do what you want to do
}
}
}
すべての Swift パイオニアに役立つことを願っています
于 2015-11-03T18:34:07.743 に答える
3
Xamarin を使用している場合:
void panGesture(UIPanGestureRecognizer gestureRecognizer) {
if (gestureRecognizer.State == UIGestureRecognizerState.Began) {
startLocation = gestureRecognizer.TranslationInView (view)
} else if (gestureRecognizer.State == UIGestureRecognizerState.Ended) {
PointF stopLocation = gestureRecognizer.TranslationInView (view);
float dX = stopLocation.X - startLocation.X;
float dY = stopLocation.Y - startLocation.Y;
float distance = Math.Sqrt(dX * dX + dY * dY);
System.Console.WriteLine("Distance: {0}", distance);
}
}
于 2014-04-29T21:55:19.683 に答える
3
func swipeAction(gesture: UIPanGestureRecognizer) {
let transition = sqrt(pow(gesture.translation(in: view).x, 2)
+ pow(gesture.translation(in: view).y, 2))
}
于 2017-08-25T11:08:21.927 に答える
2
標準的な方法でのみ行うことができます: touchBegin のタッチ ポイントを覚えて、touchEnd からのポイントを比較します。
于 2011-01-28T14:28:31.470 に答える