Skip to content

Commit

Permalink
Change method of swiping
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkowz committed Jul 25, 2015
1 parent 5d9c8fd commit 49a83a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
53 changes: 32 additions & 21 deletions Game-2048/Game-2048/GameBoardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,45 @@
import UIKit

protocol GameBoardViewDelegate {
func gameBoardView(view: GameBoardView, didSwipeInDirection direction: UISwipeGestureRecognizerDirection)
func gameBoardView(view: GameBoardView, didSwipeInDirection direction: ShiftDirection)
}

class GameBoardView: UIView {

var delegate: GameBoardViewDelegate?

override func awakeFromNib() {
super.awakeFromNib()
setupGestures()
}
private var startLocation: CGPoint = CGPointZero

private func setupGestures() {
let selector = Selector("handleSwipe:")
let directions = [
UISwipeGestureRecognizerDirection.Left,
UISwipeGestureRecognizerDirection.Right,
UISwipeGestureRecognizerDirection.Down,
UISwipeGestureRecognizerDirection.Up]
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
startLocation = touch.locationInView(self)
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let newLocation = touch.locationInView(self)
let prevLocation = startLocation

for direction in directions {
let recognizer = UISwipeGestureRecognizer(target: self, action: selector)
recognizer.direction = direction
addGestureRecognizer(recognizer)
var directionX: ShiftDirection!
if newLocation.x > prevLocation.x {
directionX = .Right
} else {
directionX = .Left
}
}

func handleSwipe(recognizer: UISwipeGestureRecognizer) {
delegate?.gameBoardView(self, didSwipeInDirection: recognizer.direction)

var directionY: ShiftDirection!
if newLocation.y > prevLocation.y {
directionY = .Down
} else {
directionY = .Up
}

var direction: ShiftDirection!
if abs(newLocation.x - prevLocation.x) > abs(newLocation.y - prevLocation.y) {
direction = directionX
} else {
direction = directionY
}

delegate?.gameBoardView(self, didSwipeInDirection: direction)
}
}
10 changes: 2 additions & 8 deletions Game-2048/Game-2048/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,8 @@ class GameViewController: UIViewController, GameBoardViewDelegate, GameLogicMana
}

// MARK: GameBoardViewDelegate
func gameBoardView(view: GameBoardView, didSwipeInDirection direction: UISwipeGestureRecognizerDirection) {
switch direction {
case UISwipeGestureRecognizerDirection.Up: gameManager.shift(.Up)
case UISwipeGestureRecognizerDirection.Right: gameManager.shift(.Right)
case UISwipeGestureRecognizerDirection.Down: gameManager.shift(.Down)
case UISwipeGestureRecognizerDirection.Left: gameManager.shift(.Left)
default: break
}
func gameBoardView(view: GameBoardView, didSwipeInDirection direction: ShiftDirection) {
gameManager.shift(direction)
}


Expand Down

0 comments on commit 49a83a5

Please sign in to comment.