2016-12-08 7 views
0
if (gesture.state == UIGestureRecognizerStateBegan) { 
    _initial = [gesture locationInView:self.view]; 
}else if (gesture.state == UIGestureRecognizerStateChanged){ 
    CGPoint p = [gesture locationInView:self.view]; 
    double dy = p.y - _initial.y; 
    if (dy > 0) { 
     NSLog(@"Finger moved to the up"); 
    } 
    else { 
     NSLog(@"Finger moved to the down"); 
    } 
} 

初期点を比較していないこれはUILongPressGestureRecognizer方向を検出するための私の方法ですが、私は が最初のポイントを比較したがのstateChangedポイントを比較していない方向を検出します。UILongPressGestureRecognizer方向を検出

フォーム0〜7は稼動していますが、7〜-5がダウンし、-5〜-2が稼働しています。

答えて

0

translationInViewを使用してください。

CGPoint translation = [gesture translationInView:view.superview]; 

if (translation.y>0) { 
// moving up 
} else { 
// moving down 
} 
+0

UILongPressGestureRecognizerは、あなたの投稿は "UIPanGestureRecognizer" と述べ –

+0

translationInViewメソッドを持っていません。そして今、あなたは長引きを言う。また、パンニングを検出することができるような、長押しの認識装置はありません。彼らは2つの異なるものです。 – GeneCode

0

私の友人が私にいくつかのアドバイスを与え、それがうまく機能:

CGPoint point = [gestureRecognizer locationInView:self.view]; 

if(gestureRecognizer.state == UIGestureRecognizerStateBegan){ 
    _lastY = point.y; 
}else if(gestureRecognizer.state == UIGestureRecognizerStateChanged) { 
    double y = point.y - _lastY; 

    if (y < 0.f) { 
     NSLog(@"****up*****"); 

    }else{ 
     NSLog(@"****down*****"); 
    } 

    _lastY = point.y; 
} 

私はそれが良いものではありません知っているが、それは動作しますが、任意のアイデアを?

0

スウィフト3

ここUILongPressGestureRecognizerの左または右の動きを決定するための完全な機能です:

@objc func swipePress (_ sender: UILongPressGestureRecognizer) { 

     let location = sender.location(in: self.view) 
     print ("Location : \(location.x)") 

     if sender.state == .ended { 
      print("swipe ended") 
      buttonCreated = false 

      animateRemoveArrows() 
      if gestureDirection == "right" { 
       print("going forward") 
       if myWebView.canGoForward { 
       myWebView.goForward() //go forward 
       } else { 

       } 
      } else if gestureDirection == "left" { 
       print("going backward") 
       if myWebView.canGoBack { 
       myWebView.goBack() //go back 
       } else { 

       } 

      } else { 

      } 


     } else if sender.state == .began { 
      gestureDirection = "" // reset direction 
      print("swipe started") 
      firstPoisitionX = location.x 
      firstPoisitionY = location.y 

      print ("Last x: \(firstPoisitionX)") 

     } else if sender.state == .changed { 

      if location.y > (firstPoisitionY + 100) { 
       print("going up or down") 

      } else if location.y < (firstPoisitionY - 100) { 
       print("going up or down") 

      } else if location.x > (firstPoisitionX + 50) { 
       print("going right") 
       if buttonCreated == false { // only create one button 
        showArrow(direction: "right") 
        print(rightArrow.center) 
        gestureDirection = "right" 
        buttonCreated = true 
       } 
      } else if location.x < (firstPoisitionX - 50) { 
       print("going left") 
       if buttonCreated == false { // only create one button 
        showArrow(direction: "left") 
        print(leftArrow.center) 

        gestureDirection = "left" 
        buttonCreated = true 
       } 
      } 
     } 
    } 
関連する問題