Esempio n. 1
0
        private async Task HandleTouchCompleted()
        {
            if (itemIndex >= ItemsSource.Count)
            {
                return;
            }

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

            // Check if card has been dragged far enough to trigger action
            if (Math.Abs(cardDistance) >= CardMoveDistance)
            {
                // Move card off the screen
                await topCard.TranslateTo(cardDistance > 0?this.Width : -this.Width, 0, animationLength / 2, Easing.SpringOut);

                topCard.IsVisible = false;

                // Fire events
                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]);
                    }
                }

                // Next card
                itemIndex++;
                ShowNextCard();
            }
            else
            {
                // Move card back to the center
                var traslateAnmimation = topCard.TranslateTo((-topCard.X), -topCard.Y, animationLength, Easing.SpringOut);
                var rotateAnimation    = topCard.RotateTo(0, animationLength, Easing.SpringOut);

                // Scale the back card down
                var scaleAnimation = backCard.ScaleTo(defaultSubcardScale, animationLength, Easing.SpringOut);

                // Run all animations from above simultaneiously
                await Task.WhenAll(new List <Task> {
                    traslateAnmimation, rotateAnimation, scaleAnimation
                });
            }

            if (itemIndex < ItemsSource.Count)
            {
                FinishedDragging?.Invoke(this, new DraggingEventArgs(ItemsSource[itemIndex]));
            }
        }
Esempio n. 2
0
        private async Task HandleTouchCompleted()
        {
            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));
            }
        }
Esempio n. 3
0
        async Task HandleTouchCompleted()
        {
            if (itemIndex >= ItemsSource.Count)
            {
                await Application.Current.MainPage.DisplayAlert("SwipeCard Demo", "Sorry there is no card left to swipe.", "OK");

                return;
            }


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

            // Check if card has been dragged far enough to trigger action
            if (Math.Abs(cardDistance) >= CardMoveDistance)
            {
                // Move card off the screen
                await topCard.TranslateTo(cardDistance > 0?this.Width * 2 : -this.Width * 2, 0, defaultAnimationLength, Easing.SinIn);

                topCard.IsVisible = false;

                // Fire events
                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]);
                    }
                }

                // Next card
                itemIndex++;
                ShowNextCard();
            }
            else
            {
                // Run animations simultaniously
                await Task.WhenAll(
                    // Move card back to the center
                    topCard.TranslateTo((-topCard.X), -topCard.Y, defaultAnimationLength, Easing.SpringOut),
                    topCard.RotateTo(0, defaultAnimationLength, Easing.SpringOut),

                    // Scale the back card down
                    backCard.ScaleTo(defaultSubcardScale, defaultAnimationLength, Easing.SpringOut)
                    );
            }

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