public async Task Swipe(SwipeDirection direction, uint animationLength = DefaultAnimationLength)
        {
            if (_itemIndex >= ItemsSource?.Count)
            {
                NoMoreCards?.Invoke(this, new EventArgs());
                return;
            }

            var topCard  = CardStack.Children[NumberOfCards - 1];
            var backCard = CardStack.Children[NumberOfCards - 2];

            Swiped?.Invoke(this, new SwipedEventArgs(ItemsSource[_itemIndex], direction));

            if (direction == SwipeDirection.Left)
            {
                if (SwipedLeftCommand != null && SwipedLeftCommand.CanExecute(ItemsSource[_itemIndex]))
                {
                    SwipedLeftCommand.Execute(ItemsSource[_itemIndex]);
                }
            }
            else if (direction == SwipeDirection.Right)
            {
                if (SwipedRightCommand != null && SwipedRightCommand.CanExecute(ItemsSource[_itemIndex]))
                {
                    SwipedRightCommand.Execute(ItemsSource[_itemIndex]);
                }
            }

            _itemIndex++;

            await Task.WhenAll(

                topCard.TranslateTo(direction == SwipeDirection.Right ? Width * 2 : -Width * 2, 0, animationLength, Easing.SinIn),
                topCard.RotateTo(direction == SwipeDirection.Right ? 17.18873385f : -17.18873385f, animationLength, Easing.SinIn),
                backCard.ScaleTo(1.0f, animationLength),
                backCard.TranslateTo(0, 0, animationLength, Easing.SinIn),
                backCard.FadeTo(1, animationLength, Easing.SinIn)
                );

            topCard.IsVisible = false;

            ShowNextCard();
        }
Example #2
0
        public async Task HandleTouchCompleted()
        {
            if (_itemIndex >= ItemsSource.Count - 1)
            {
                NoMoreCards?.Invoke(this, new EventArgs());
            }

            if (_itemIndex >= ItemsSource.Count || !_isDragging)
            {
                return;
            }

            _lastX      = 0;
            _isDragging = false;

            var topCard  = CardStack.Children[NumberOfCards - 1];
            var backCard = CardStack.Children[NumberOfCards - 2];

            if (Math.Abs(_cardDistance) >= CardMoveDistance)
            {
                await topCard.TranslateTo(_cardDistance > 0?Width * 2 : -Width * 2, 0, DefaultAnimationLength, Easing.SinIn);

                topCard.IsVisible = false;

                if (_cardDistance > 0)
                {
                    Swiped?.Invoke(this, new SwipedEventArgs(ItemsSource[_itemIndex], SwipeDirection.Right));

                    if (SwipedRightCommand != null && SwipedRightCommand.CanExecute(ItemsSource[_itemIndex]))
                    {
                        SwipedRightCommand.Execute(ItemsSource[_itemIndex]);
                    }
                }
                else
                {
                    Swiped?.Invoke(this, new SwipedEventArgs(ItemsSource[_itemIndex], SwipeDirection.Left));

                    if (SwipedLeftCommand != null && SwipedLeftCommand.CanExecute(ItemsSource[_itemIndex]))
                    {
                        SwipedLeftCommand.Execute(ItemsSource[_itemIndex]);
                    }
                }


                _itemIndex++;

                ShowNextCard();
            }
            else
            {
                await Task.WhenAll(

                    topCard.TranslateTo((-topCard.X), -topCard.Y, DefaultAnimationLength, Easing.SpringOut),
                    topCard.RotateTo(0, DefaultAnimationLength, Easing.SpringOut),
                    backCard.ScaleTo(_defaultSubcardScale, DefaultAnimationLength, Easing.SpringOut),
                    backCard.TranslateTo(_defaultSubcardTranslationX, 0, DefaultAnimationLength, Easing.SpringOut),
                    backCard.FadeTo(_defaultSubcardOpacity, DefaultAnimationLength, Easing.SpringOut)
                    );
            }

            _cardDistance = 0;

            if (_itemIndex < ItemsSource.Count)
            {
                FinishedDragging?.Invoke(this, new DraggingEventArgs(ItemsSource[_itemIndex], _cardDistance));
            }
        }