Search code examples
iosobjective-canimationswipegesture

How to do smooth swipe animation in ios


I want to do smooth swipe animation. I just want to that swipe only can be possible when user swipe the page from the right or left border only. Middle of the page swipe should not possible.Both the swipe should be possible left to right and right to left.

I have tried lots of swipe animation sample code or demo code. But its not what I want. I want animation like this https://itunes.apple.com/in/app/clear-tasks-to-do-list/id493136154?mt=8

In this app its like when we touch the right border its swipe smoothly.Please guide me to do this animation. Thanks in advance.


Solution

  • Sorry for the late reply. Just saw this question.

    If you want your swipe operation to happen from the edges, create 2 subviews in the far ends (left and right) of your main view and give then a width of 30 or 40.

    enter image description here

    I believe you have 2 other views popin up from left and right. So inorder to do this you need to add 2 views right on top of your main view. Now for the left view, set it's right horizondal space constraint connecting to the main view to a value lesser than (-1)x width of the main view. For the right view set its right horizondal space constraint connecting to the main view to a value greater than the width of the main view, so that both the views are outside the main view

    X stands for a value greater than or equal to the mainview's width

    X stands for a value greater than or equal to the mainview's width

    Add two NSLayoutConstraint variables as IBOutlet holding these 2 values.

    NSLayoutConstraint *leftViewHorizondalRightPadding;
    NSLayoutConstraint *rightViewHorizondalRightPadding;
    

    Now add the UISwipeGestures to these subViews (indicated in orange).

    UISwipeGestureRecognizer *leftToRightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
        [leftToRightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
        [self.leftSubview addGestureRecognizer:leftToRightSwipe];
    
     UISwipeGestureRecognizer *rightToLeftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
        [rightToLeftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
        [self.rightSubview addGestureRecognizer:rightToLeftSwipe];
    
    ///Now in the swipe handler distinguish the swipe actions
    -(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
        NSLog(@"Swipe received.");
        if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        //It's leftToRight
           leftViewHorizondalRightPadding.constant = 0;
           [UIView animateWithDuration:1
               animations:^{
               [self.view layoutIfNeeded]; 
           }];
    
        }
        else {
        //It's rightToLeft
           rightViewHorizondalRightPadding.constant = 0;
           [UIView animateWithDuration:1
               animations:^{
               [self.view layoutIfNeeded]; 
           }];
    
        }
    }
    

    }

    This will make a swipe animation from left to right and right to left.

    Hope this helps..