1

I've created a really basic version of what I've got so far in a JSFiddle, which can be found here: http://jsfiddle.net/hamchapman/a97Yq/5/

This is the code in case the JSFiddle doesn't work:

// ** view **

<div class="container" ng-controller="AdminCtrl">
    <div class="tweet-list">
        <div class="tweet" ng-repeat="tweet in tweets" ng-class="{ 'swipe-left': $index == activeIndexLeft, 'swipe-right': $index == activeIndexRight }">
            <div ng-swipe-right="discardTweet(tweet, $index)" ng-swipe-left="verifyTweet(tweet, $index)">{{tweet.text}}</div>
            <button ng-click="verifyTweet(tweet, $index)" type="button">Show Tweet</button>
            <button ng-click="discardTweet(tweet, $index)" type="button">Discard Tweet</button>
        </div>
    </div>
</div>

// ** style **

.swipe-left.ng-leave, .swipe-right.ng-leave {
    -webkit-transition: 500ms cubic-bezier(0.420, 0.000, 1.000, 1.000) all;
    -moz-transition: 500ms cubic-bezier(0.420, 0.000, 1.000, 1.000) all;
    -ms-transition: 500ms cubic-bezier(0.420, 0.000, 1.000, 1.000) all;
    -o-transition: 500ms cubic-bezier(0.420, 0.000, 1.000, 1.000) all;
    transition: 500ms cubic-bezier(0.420, 0.000, 1.000, 1.000) all;
}
.swipe-left.ng-leave {
    left: 0;
}
.swipe-left.ng-leave.ng-leave-active {
    position: absolute;
    left: -100%;
}
.swipe-right.ng-leave {
    left: 0;
}
.swipe-right.ng-leave.ng-leave-active {
    position: absolute;
    left: 100%;
}

// ** angular **

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

function AdminCtrl($scope) {
    $scope.activeIndexLeft = -1;
    $scope.activeIndexRight = -1;
    $scope.tweets = [{
        text: "tester"
    }, {
        text: "tester 2"
    }, {
        text: "tester 3"
    }, {
        text: "tester 4"
    }, {
        text: "tester 5"
    }, {
        text: "tester 6"
    }];

    $scope.verifyTweet = function (tweet, $index) {
        $scope.activeIndexLeft = $index;
        var i = $scope.tweets.indexOf(tweet);
        if (i != -1) {
            $scope.tweets.splice(i, 1);
        }
    };

    $scope.discardTweet = function (tweet, $index) {
        $scope.activeIndexRight = $index;
        var i = $scope.tweets.indexOf(tweet);
        if (i != -1) {
            $scope.tweets.splice(i, 1);
        }
    }
};

You can see that it's what feels like a fairly hacked together method and it doesn't work perfectly either. No animation happens on the first click (or swipe) of an item at a given index (because the swipe-left/right class is only applied when clicked (or swiped).

It's generally pretty buggy and it doesn't seem like the way to do it using Angular.

What is a better way of achieving the differing animations based on the swipe direction (or which button is clicked)?

hamchapman
  • 2,901
  • 4
  • 22
  • 37
  • Facing the exact same problem right now where ng-class does not happen till I perform an ng-click. Trying to do swiping on mobile devices without buttons. So far this one is a real pain. Also my ng-class="slideDir", which is $scope.slideDir. My guess is that the variables don't exist yet. – Blackunknown Apr 07 '14 at 23:40

0 Answers0