ceth
ceth

Reputation: 45295

Swipe gesture as JS event in PhoneGap

I have developed my own version of 2048 game (http://gabrielecirulli.github.io/2048/) using AngularJS.

To play the game I use ng-keydown directive:

<body ng-app="game2048" ng-controller="MainController" ng-keydown="key_pressed($event)">

And controller:

$scope.key_pressed = function(e) {
    if (e.keyCode === 37) {
        $scope.grid.moveLeft();
    } else if (e.keyCode === 38) {
        $scope.grid.moveUp();
    } else if (e.keyCode === 39) {
        $scope.grid.moveRight();
    } else if (e.keyCode === 40) {
        $scope.grid.moveDown();
    }

    $scope.grid.generateRandomCell();
};

Is there any way to catch swipe events on iPhone application an translate them to AngularJS calls()?

I want to swipe down and call $scope.grid.moveDown() automatically.

Upvotes: 2

Views: 1271

Answers (1)

Thomas Stock
Thomas Stock

Reputation: 11266

I assume you are already using ngTouch, AngularJS's gesture support service.

I found some directives that do what you need: https://github.com/marmorkuchen-net/angular-swipe

Usage:

    <div class="container" ng-swipe-down="swipe($event)">
      <h1>Swipe me up!</h1>
    </div>

And in the JavaScript include the Swipe library

var myApp = angular.module('myApp', [ 'swipe' ]);

Upvotes: 2

Related Questions