Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7535471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:18:31+00:00 2026-05-30T06:18:31+00:00

Im struggling with some iOS development and I hope someone will be able to

  • 0

Im struggling with some iOS development and I hope someone will be able to help me with an advice. I want to implement a stack of UIViews that will look like a deck of cards. User should be able to swipe out ‘cards’ with touch. I thought of UIScrollViews with pagingEnabled to give an impression that cards are separate. However, UIScrollView can swipe from horizontally where something appears from left or right side. I want my deck of cards to be in the middle of the screen so the user can swipe cards from the deck without seeing the card appearing on the left or right side.
Also, I was thinking of using Touch Methods ( touchesBegan, touchesMoved, etc.) to move views. It could work… but Im worried that I wont be able to reproduce proper mechanics of the card ( friction, bouncing effect when the swipe is to short and so on..).

Can someone please point me to a proper technique or advice some tutorials? I did some research already but I couldn’t find anything that would be helpful.

Here is an image of the effect that I would like to achieve.

enter image description here

And what I want to achieve is to swipe cards out of the deck.

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:18:32+00:00Added an answer on May 30, 2026 at 6:18 am

    I guess you want a swipe to take the top card of the stack and “shuffle” it to the bottom of the stack, revealing the second card on the stack, like this:

    card moving from top to bottom of deck

    The animation is much smoother in real life. I had to use a low frame rate to make the animated GIF small.

    One way to do this is to create a subclass of UIView that manages the stack of card views. Let’s call the manager view a BoardView. We’ll give it two public methods: one for shuffling the top card to the bottom, and one for shuffling the bottom card to the top.

    @interface BoardView : UIView
    
    - (IBAction)goToNextCard;
    - (IBAction)goToPriorCard;
    
    @end
    
    @implementation BoardView {
        BOOL _isRestacking;
    }
    

    We’ll use the _isRestacking variable to track whether the board view is animating the movement of a card off to the side.

    A BoardView treats all of its subviews as card views. It takes care of stacking them up, with the top card centered. In your screen shot, you offset the lower cards by slightly randomized amounts. We can make it look a little sexier by rotating the lower cards randomly. This method applies a small random rotation to a view:

    - (void)jostleSubview:(UIView *)subview {
        subview.transform = CGAffineTransformMakeRotation((((double)arc4random() / UINT32_MAX) - .5) * .2);
    }
    

    We’ll want to apply that to each subview as it’s added:

    - (void)didAddSubview:(UIView *)subview {
        [self jostleSubview:subview];
    }
    

    The system sends layoutSubviews whenever a view’s size changes, or when a view has been given new subviews. We’ll take advantage of that to lay out all of the cards in a stack in the middle of the board view’s bounds. But if the view is currently animating a card out of the way, we don’t want to do the layout because it would kill the animation.

    - (void)layoutSubviews {
        if (_isRestacking)
            return;
        CGRect bounds = self.bounds;
        CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
        UIView *topView = self.subviews.lastObject;
        CGFloat offset = 10.0f / self.subviews.count;
        for (UIView *view in self.subviews.reverseObjectEnumerator) {
            view.center = center; 
            if (view == topView) {
                // Make the top card be square to the edges of the screen.
                view.transform = CGAffineTransformIdentity;
            }
            center.x -= offset;
            center.y -= offset;
        }
    }
    

    Now we’re ready to handle the shuffling. To “go to the next card”, we need to animate moving the top card off to the side, then move it to the bottom of the subview stacking order, and then animate it back to the middle. We also need to nudge the positions of all of the other cards because they’ve all moved closer to the top of the stack.

    - (void)goToNextCard {
        if (self.subviews.count < 2)
            return;
    

    First, we animate the movement of the top card off to the side. To make it look sexy, we rotate the card as we move it.

        UIView *movingView = self.subviews.lastObject;
        [UIView animateWithDuration:1 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{
            _isRestacking = YES;
            CGPoint center = movingView.center;
            center.x = -hypotf(movingView.frame.size.width / 2, movingView.frame.size.height / 2);
            movingView.center = center;
            movingView.transform = CGAffineTransformMakeRotation(-M_PI_4);
        }
    

    In the completion block, we move the card to the bottom of the stack.

        completion:^(BOOL finished) {
            _isRestacking = NO;
            [self sendSubviewToBack:movingView];
    

    And to move the now-bottom card back into the stack, and nudge all of the other cards, we’ll just call layoutSubviews. But we’re not supposed to call layoutSubviews directly, so instead we use the proper APIs: setNeedsLayout followed by layoutIfNeeded. We call layoutIfNeeded inside an animation block so the cards will be animated into their new positions.

            [self setNeedsLayout];
            [UIView animateWithDuration:1 delay:0 options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
                [self jostleSubview:movingView];
                [self layoutIfNeeded];
            } completion:nil];
        }];
    }
    

    That’s the end of goToNextCard. We can do goToPriorCard similarly:

    - (void)goToPriorCard {
        if (self.subviews.count < 2)
            return;
        UIView *movingView = [self.subviews objectAtIndex:0];
        [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _isRestacking = YES;
            CGPoint center = movingView.center;
            center.x = -movingView.frame.size.height / 2;
            movingView.center = center;
            movingView.transform = CGAffineTransformMakeRotation(-M_PI_4);
        } completion:^(BOOL finished) {
            _isRestacking = NO;
            UIView *priorTopView = self.subviews.lastObject;
            [self bringSubviewToFront:movingView];
            [self setNeedsLayout];
            [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
                [self jostleSubview:priorTopView];
                [self layoutIfNeeded];
            } completion:nil];
        }];
    }
    

    Once you have BoardView, you just need to attach a swipe gesture recognizer that sends it the goToNextCard message, and another swipe gesture recognizer that sends it the goToPriorCard message. And you need to add some subviews to act as cards.

    Another detail: to get the edges of the cards to look smooth when they’re jostled, you need to set UIViewEdgeAntialiasing to YES in your Info.plist.

    You can find my test project here: http://dl.dropbox.com/u/26919672/cardstack.zip

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling with some Generic constraint issues when trying to implement a library that
I'm actually struggling with some piece of code. I do know that it can
I've been struggling for some time now to be able to use the built-in
I am struggling alot with some PHP I am needing to implement a next
After some struggling I managed to get boost smart pointers to build for Windows
I'm struggling getting some unit tests running and wondering if anyone might have anything
Hi I'm struggling with some regex I've got a string like this: a:b||c:{d:e||f:g}||h:i basically
For some reason I'm really struggling with this. I'm new to wpf and I
I have been struggling to think of some decent uses for things like vectors
I have been struggling with this for quite some time having been accustomed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.