检测UILongPressGestureRecognizer方向而不是比较初始点

时间:2016-12-08 03:15:47

标签: ios objective-c algorithm uigesturerecognizer

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点。

很难描述,比如:表格0到7是向上的,但是7到-5是向下的,然后是-5到-2向上。

3 个答案:

答案 0 :(得分:0)

您应该使用translationInView。

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

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

答案 1 :(得分: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;
}

我知道它不是一个好的但它有效,任何想法?

答案 2 :(得分:0)

Swift 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
                }
            }
        }
    }