0

I am creating a game, where you control using swipes and I wanted to know if it is possible to handle sideways swipes using UIGestureRecodnizer?

If so how do I do it.

In other word:

Instead of having 4 directions, how do I get 8:

From up,down,right,left to upleft,upright,downright,downleft,up,down,right,left ?

Also how do can I properly differ between a double swipe and a two single swipes, does UIGestureRecognizer have an inbuilt timeout?

Nocturne
  • 31
  • 6

1 Answers1

1

This could use some cleaning up but it works:

// 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")
              }
    }
    
}
Brandon Bravos
  • 390
  • 1
  • 2
  • 10
  • If you have problems with it let me know, I made it on my break so it’s sloppy, but when I get home I’ll make it as an extension for you so it’s easier to use / clean the code up – Brandon Bravos Jul 28 '20 at 03:31
  • I am right to understand that I add this an extension to the GameViewController and than send the variables to SKScene? – Nocturne Jul 29 '20 at 00:38
  • Ok, for anyone wanting to use this in SKScene direction are moved 180 degrees, so it goes down, downRight, right, upRight, up, upLeft, left, downLeft, downLeft, left – Nocturne Jul 29 '20 at 01:00
  • No, it’s a UI view for now, sort of like a canvas in unity. did you get it to work? You’d have had to add the view! – Brandon Bravos Jul 29 '20 at 04:16
  • I have just transferred everything into a GameScene since it also has touchesBegan/Ended methods, but to it the 180 rules applies, but it still works.Thank you for your help! – Nocturne Jul 31 '20 at 22:43