如何处理横向滑动

How to handle sideways swipes

我正在创建一个游戏,您可以在其中使用滑动进行控制,我想知道是否可以使用 UIGestureRecodnizer 处理横向滑动?

如果可以,我该怎么做。

换句话说:

我如何得到 8 个而不是 4 个方向:

从上、下、右、左到左上、正、下、左下、上、下、右、左?

此外,我如何正确区分两次滑动和两次单次滑动,UIGestureRecognizer 是否有内置超时?

这可能需要一些清理工作,但它有效:

// a uiview that tracks touches in 8 directions when swiped
class TouchRecognizer: UIView{
    
 // used for calculating degree between touches
    var startTouch: CGPoint = CGPoint(x: 0, y: 0)
    var endTouch: CGPoint = CGPoint(x: 0, y: 0)
    
    // gets initial touch
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let location = touch.location(in: self)
    
        startTouch = location
    }
    
    // gets end touch, then checks angle
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let location = touch.location(in: self)
        endTouch = location
       
        HandleSwipe()
   
    }
    
    // Gets The Degree between the two touches
    func HandleSwipe(){
        let p1 = startTouch
        let p2 = endTouch
        
        let center = CGPoint(x: (p1.x), y: (p1.y + 1))
        let v1 = CGVector(dx: p1.x - center.x, dy: p1.y - center.y)
        let v2 = CGVector(dx: p2.x - center.x, dy: p2.y - center.y)
        let angle = atan2(v2.dy, v2.dx) - atan2(v1.dy, v1.dx)
        let deg = angle * CGFloat(180.0 / .pi)
        
        PrintDirection(degree: deg)
    }
    
    
    // uses the degree between touches to figure out the direction user swiped
    func PrintDirection(degree: CGFloat){
        
        // if needed, adjust degree angles for optimized swiping
        if(degree < 30 && degree > -10){
            print("Up")
        }
        else if(degree > 30 && degree < 60){
            print("UpRight")
        }
        else if(degree > 75 && degree < 120){
            print("Right")
        }
        else if(degree > 120 && degree < 150){
            print("downRight")
        }
        else if(degree > 150 && degree < 210){
            print("Down")
        }
        else if(degree > 210 && degree < 240){
            print("downleft")
        }
        else if(degree > 240 && degree < 300){
            print("Left")
        }
        else if(degree > 300 && degree < 360){
            print("upLeft")
        }
        else if(degree < -30 && degree > -60){
            print("upLeft")
        }
        else if(degree < -60){
                  print("left")
              }
    }
    
}