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)
                {
                    SwipedRight?.Invoke(ItemsSource[itemIndex]);
                    if (SwipedRightCommand != null && SwipedRightCommand.CanExecute(ItemsSource[itemIndex]))
                    {
                        SwipedRightCommand.Execute(ItemsSource[itemIndex]);
                    }
                }
                else
                {
                    SwipedLeft?.Invoke(ItemsSource[itemIndex]);
                    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(ItemsSource[itemIndex]);
            }
        }
示例#2
0
        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 * 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]));
            }
        }
示例#3
0
        public async void Swipe(SwipeDirection direction, uint animationLength = defaultAnimationLength)
        {
            // Check if there is something to swipe
            if (itemIndex >= ItemsSource?.Count)
            {
                return;
            }

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

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

            // Increase item index
            // Do that before the animation runs
            itemIndex++;

            // Animate card
            await Task.WhenAll(
                // Move card left or right
                topCard.TranslateTo(direction == SwipeDirection.Right ? this.Width * 2 : -this.Width * 2, 0, animationLength, Easing.SinIn),

                // Rotate card (57.2957795f/3=17.18873385f)
                topCard.RotateTo(direction == SwipeDirection.Right ? 17.18873385f : -17.18873385f, animationLength, Easing.SinIn),

                // Scale back card up
                backCard.ScaleTo(1.0f, animationLength)
                );

            topCard.IsVisible = false;

            // Next card
            ShowNextCard();
        }
示例#4
0
        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();
        }
示例#5
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));
            }
        }