Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Present/create view controllers with swipe

I have UIViewController with fabric method "create with index".

What I want to build is just like photo gallery in iPhone, you swipe left - you create view controller with index -1, you swipe right, you create another controller with index +1.

I used UIPageViewController for that, but for some reason it show me wrong page after certain swipes, and I cant figure out why.

What my outcome is, I just want to use Swipe Gesture to create my controllers with swipes.

But, I need animation, like when you scroll one page left or right, so, you can see with animation how new controller is presented, and show "old" controller in animation time. Now i use:

-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
    //Do what you want here
    NSLog(@"did swiped left");
    [CalendarViewController createNext:-1];
    [self presentViewController:[CalendarViewController createNext:-1] animated:YES completion:nil];

}

-(void)swiperight:(UISwipeGestureRecognizer*)gestureRecognizer
{
    //Do what you want here
    NSLog(@"did swiped right");
    [CalendarViewController createNext:[CalendarViewController createNext:1]];
    [self presentViewController:[CalendarViewController createNext:1] animated:YES completion:nil];

}

Is there any way to achieve animation, like when you swipe photo in gallery left or right? Now it look like modally presented new vc.

Upvotes: 0

Views: 876

Answers (2)

Ketan Parmar
Ketan Parmar

Reputation: 27428

If you want to add effect as you mentioned in question then you have to add two swipe gesture recognizer for detection of left swipes and right swipes.

You should have configure your own transition effect which presents your viewcontroller like push or pop effect. You can achieve it by following,

For presenting View Controller (swipe from right to left),

CATransition *customTransition = [CATransition animation];
customTransition.duration = 0.3;
customTransition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
customTransition.type = kCATransitionPush;
customTransition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:customTransition forKey:nil];

[self presentViewController:yourViewControllerToBePresent animated:YES completion:nil];

For Dismissing View Controller(swipe from left to right i assume!),

  CATransition *customTransitionDismiss = [CATransition animation];
customTransitionDismiss.duration = 0.3;
customTransitionDismiss.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
customTransitionDismiss.type = kCATransitionPush;
customTransitionDismiss.subtype = kCATransitionFromLeft;
[self.view.window.layer addAnimation:customTransitionDismiss forKey:nil];
[self dismissViewControllerAnimated:YES completion:nil];

But this is not best approach i think.

If you have many pages then you should go with UIPageViewController or UICollectionView with single cell with paging scroll effect which will deque the cells and will be memory efficient!!

Upvotes: 1

Chirag Patel
Chirag Patel

Reputation: 1481

try below code for left swipe use transition subtype kCATransitionFromLeft what you want

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let vc = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
        let transition: CATransition = CATransition()
        transition.duration = 0.5
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        transition.type = kCATransitionReveal
        transition.subtype = kCATransitionFromRight
        self.view.window!.layer.addAnimation(transition, forKey: nil)
        self.presentViewController(vc, animated:false, completion:nil)

Upvotes: 1

Related Questions