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沒有translationInView方法 –

+0

您的文章說, 「UIPanGestureRecognizer」。現在你說longpress。也沒有這種東西longpressrecognizer,也可以檢測平移。他們都是兩個不同的東西。 – 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 
       } 
      } 
     } 
    }