单手旋转手势与迅捷3

时间:2017-07-19 13:22:55

标签: ios swift swift3 uigesturerecognizer

我想知道如何识别单指旋转手势(当有人在屏幕上画圆圈时)。

我已经看到this class完美无缺,这正是我想要做的事情(调光器或类似的东西)但是我不能在我的应用程序中使用它,因为它超越了另一个手势而且我不能再认出他们了(我从来没有离开过课堂上的东西并回到我的班级)。

所以我想知道你是否可以给我一些帮助来识别这个手势(我的应用程序的最后一个......)

我已经能够识别水龙头,滑动,捏,长按,这是我唯一想念的。

以下是我的代码示例:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.timer.invalidate()
    isPinched = false
    super.touchesBegan(touches, with: event)
    startTime = getCurrentMillis()

    for touch in touches{
        let point = touch.location(in: self.view)
        for (index,finger)  in fingers.enumerated() {
            if finger == nil {
                fingers[index] = String(format: "%p", touch)

                if (finger1.isEmpty){
                    finger1 = [point.x, point.y]
                } else if (finger2.isEmpty){
                    finger2 = [point.x, point.y]
                } else if (finger3.isEmpty){
                    finger3 = [point.x, point.y]
                } else if (finger4.isEmpty){
                    finger4 = [point.x, point.y]
                } else if (finger5.isEmpty){
                    finger5 = [point.x, point.y]
                } else if (finger6.isEmpty){
                    finger6 = [point.x, point.y]
                } else if (finger7.isEmpty){
                    finger7 = [point.x, point.y]
                } else if (finger8.isEmpty){
                    finger8 = [point.x, point.y]
                } else if (finger9.isEmpty){
                    finger9 = [point.x, point.y]
                } else if (finger10.isEmpty){
                    finger10 = [point.x, point.y]
                }
                break
            }
        }
    }

    //Where I call the function when the touch is long enough, I recognize longPress and rotation in it

    timer = Timer.scheduledTimer(timeInterval: 0.35, target: self, selector: #selector(increaseValue), userInfo: nil, repeats: true)
    if ascending {
        ascending = false
    }
    else {
        ascending = true
    }
}

// Keep track of each finger and register the position

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)

    for touch in touches {
        let point = touch.location(in: self.view)
        for (index,finger) in fingers.enumerated() {
            if let finger = finger, finger == String(format: "%p", touch) {
                switch (index){
                case 0 :
                    finger1 += [point.x, point.y]
                case 1 :
                    finger2 += [point.x, point.y]
                case 2 :
                    finger3 += [point.x, point.y]
                case 3 :
                    finger4 += [point.x, point.y]
                case 4 :
                    finger5 += [point.x, point.y]
                case 6 :
                    finger6 += [point.x, point.y]
                case 7 :
                    finger7 += [point.x, point.y]
                case 8 :
                    finger8 += [point.x, point.y]
                case 9 :
                    finger9 += [point.x, point.y]
                case 10 :
                    finger10 += [point.x, point.y]
                default :
                    break
                }
            }
        }
    }
}


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesEnded(touches, with: event)
    endTime = getCurrentMillis()

    for touch in touches {
        for (index,finger) in fingers.enumerated() {
            if let finger = finger, finger == String(format: "%p", touch) {
                fingers[index] = nil
                break
            }
        }
    }

    direction[0] = ""
    direction[1] = ""
    direction[2] = ""
    direction[3] = ""
    direction[4] = ""

    if finger1.count != 0 {
        direction[0] = GestureRecognizer(coordinates: finger1, index: 0)
    }
    if finger2.count != 0 {
        direction[1] = GestureRecognizer(coordinates: finger2, index: 1)
    }
    if finger3.count != 0 {
        direction[2] = GestureRecognizer(coordinates: finger3, index: 2)
    }
    if finger4.count != 0 {
        direction[3] = GestureRecognizer(coordinates: finger4, index: 3)
    }
    if finger5.count != 0 {
        direction[4] = GestureRecognizer(coordinates: finger5, index: 4)
    }

    if Int64(endTime - startTime) < 400 {
// Recognize Swipe, tap, double tap, etc
    }

DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
        self.directionLabel.text = self.labelUptade
        self.finger1 = []
        self.finger2 = []
        self.finger3 = []
        self.finger4 = []
        self.finger5 = []
        self.finger6 = []
        self.finger7 = []
        self.finger8 = []
        self.finger9 = []
        self.finger10 = []
    }
}

func increaseValue() -> Int {

    labelUptade = String(value)

    if (Conditions){
        //Recognition of long press
    } 
    else if !finger1.isEmpty {

        let a: CGFloat = finger1[0] - CGPoint.zero.x
        let b: CGFloat = finger1[1] - CGPoint.zero.y
        let c: CGFloat = finger1[finger1.count - 2] - CGPoint.zero.x
        let d: CGFloat = finger1[finger1.count - 1] - CGPoint.zero.y

        let atanA: CGFloat = atan2(a, b)
        let atanB: CGFloat = atan2(c, d)

        // convert radiants to degrees
        angle = (Float(atanA - atanB) * 360 / Float.pi) + 180
        print("angle = \(angle)")

        angle1 += angle
        print("angle1 = \(angle1)")
    }
    return value
}

这是我到目前为止所做的。正如你所看到的那样,每个0.350秒都会调用这个函数,这可能是个问题吗?我很高兴听到任何建议。

感谢您的帮助。

0 个答案:

没有答案